i am trying to pass an array of struct exported from a c# library to c++ code. the objective is to pass SAFEARRAY of struct from c++ to c#.
I have followed instructions from
http://limbioliong.wordpress.com/2011/07/16/marshaling-a-safearray-of-managed-structures-by-com-interop-part-2/
but getting this error with the call GetRecordInfoFromTypeInfo
0x80028019 Old format or invalid type library.
If you don't need to have a dispinterface, following should work:
void MyMethod([MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] MyStruct[] data, long size);
If you need to use SAFEARRAY, I would expect following to work (but I am not 100% sure, as I don't have Windows machine available at the moment):
void MyMethod([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_USERDEFINED)] MyStruct[] data);
If you can afford to change your struct to class then this will also work, and save you from UDT hassle:
void MyMethod([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_UNKNOWN)] MyStruct[] data);
Does the struct contains strings? If so, make sure to tag them [MarshalAs(UnmanagedType.BStr)]. There is a limitation in COM: strings in arrays of structs must be BSTRs. TLBExp defaults to LPWSTR and then the call dies. See here: http://msdn.microsoft.com/en-us/library/z6cfh6e6(v=vs.100).aspx
Related
I want to write an application with csharp which reads the content of a file and passes it to an ATL COM object.
I have a problem with IDL. I defined a method named "Decode" on an ATL-COM object like below:
[id(101), helpstring("Decode")] HRESULT Decode([in] BYTE* pBuff, [in] INT nLen, [out, retval] INT* pnRetVal);
But, when I compiled my own COM dll and wanted to use it in the CSharp application I found the method with this signature:
int MyClass.Decode(ref byte pBuff, int nLen);
But, I excepted to have a method like this:
int MyClass.Decode(byte[] pBuff, int nLen);
Do you have any suggestions? I also tried different types such as VARIANT* or BYTE**, but I look for best solution.
According to this link, you have to declare your function as taking a SAFEARRAY(byte) inside the IDL.
Also, passing a VARIANT (which shows as object in the C# side) will work, provided you extract the SAFEARRAY inside the C++ code.
I need to use a DLL coded in C++ in a C# application, there is a method in the DLL with receives a object declared in the C++ and I don't know how to parse a equivalent object maded in C# to the function of the DLL
Can someone help me?
You will need to use the System.Runtime.InteropServices namespace with Platform Invoke if the c++ code is not managed or COM. You will need to define the structure in your C# application and pass an object of whatever type your structure is to the method you are using Platform Invoke on. To tell you anymore, I need to see some header files.
Here is a sample :
c++ header:
struct MyStruct
{
int size;
char* data;
}
bool MethodToInvoke(MyStruct* obj);
c# declarations:
struct MyStruct
{
int size;
string data;
}
[DllImport("YourDllHere.dll")]
static extern bool MethodToInvoke(ref MyStruct obj);
Of course, it could get much more complicated but that is the general idea.
It can and may get really nasty when you have to worry about calling conventions and marshaling arrays. Regardless the world needs to see some code from you.
So what I have is a C++ API contained within a *.dll and I want to use a C# application to call methods within the API.
So far I have created a C++ / CLR project that includes the native C++ API and managed to create a "bridge" class that looks a bit like the following:
// ManagedBridge.h
#include <CoreAPI.h>
using namespace __CORE_API;
namespace ManagedAPIWrapper
{
public ref class Bridge
{
public:
int bridge_test(void);
int bridge_test2(api_struct* temp);
}
}
.
// ManagedBridge.cpp
#include <ManagedBridge.h>
int Bridge::bridge_test(void)
{
return test();
}
int Bridge::bridge_test2(api_struct* temp)
{
return test2(temp);
}
I also have a C# application that has a reference to the C++/CLR "Bridge.dll" and then uses the methods contained within. I have a number of problems with this:
I can't figure out how to call bridge_test2 within the C# program, as it has no knowledge of what a api_struct actually is. I know that I need to marshal the object somewhere, but do I do it in the C# program or the C++/CLR bridge?
This seems like a very long-winded way of exposing all of the methods in the API, is there not an easier way that I'm missing out? (That doesn't use P/Invoke!)
EDIT: Ok, so I've got the basics working now thanks to responses below, however my struct (call it "api_struct2" for this example) has both a native enum and union in the C++ native code, like the following:
typedef struct
{
enum_type1 eEnumExample;
union
{
long lData;
int iData;
unsigned char ucArray[128];
char *cString;
void *pvoid;
} uData;
} api_struct2;
I think I have figured out how to get the enum working; I've re-declared it in managed code and am performing a "native_enum test = static_cast(eEnumExample)" to switch the managed version to native.
However the union has got me stumped, I'm not really sure how to attack it.. Ideas anyone?
Yes, you are passing an unmanaged structure by reference. That's a problem for a C# program, pointers are quite incompatible with garbage collection. Not counting the fact that it probably doesn't have the declaration for the structure either.
You can solve it by declaring a managed version of the structure:
public value struct managed_api_struct {
// Members...
};
Now you can declare the method as
int bridge_test2(managed_api_struct temp); // pass by value
or
int bridge_test2(managed_api_struct% temp); // pass by reference
Pick the latter if the structure has more than 4 fields (~16 bytes). The method needs to copy the structure members, one-by-one, into an unmanaged api_struct and call the unmanaged class method. This is unfortunately necessary because the memory layout of a managed structure is not predictable.
This is all pretty mechanical, you might get help from SWIG. Haven't used it myself, not sure if it is smart enough to deal with a passed structure.
A completely different approach is to make the wrapper class cleaner by giving it a constructor and/or properties that lets you build the content of an api_struct. Or you could declare a wrapper ref class for the structure, much like you would in managed code.
as it has no knowledge of what a api_struct actually is
You need to define a managed version in a .NET assembly, that uses attributes (like StructLayoutAttribute) to ensure it marshals correctly.
This seems like a very long-winded [...]
The other approach is to create a COM wrapper (e.g. using ATL) around your API. This might be more effort, but at least you avoid the double coding of struct and function definitions needed for P/Invoke.
Correction: You have created a C++/CLI project: so just add correct '#pragma' to tell the compiler this is .NET code, and then the output is an assembly the C# project can just reference.
Yuo are trying to do this way more complicated that it really is. What you want is two different structs. One managed and one unmanaged. You expose the managed version externally (to your C# app). It will be all ".Net-ish" with no concepts of unions or so.
In your bridge you receive the managed version of the struct, manually create the unmanaged struct and write code to move your data, field by field over to the unmanaged struct. Then call your unmanaged code and finally move the data back to the managed struct.
The beautiful thing about C++/CLI is that the managed code also can work with unmanaged code and data (and include the unmanaged .h files).
I tried different things but i'm getting mad with Interop.
(here the word string is not referred to a variabile type but "a collection of char"):
I have an unmanaged C++ function, defined in a dll, that i'm trying to access from C#, this function has a string parameter and a string return value like this:
string myFunction(string inputString)
{
}
What should be string in C++ side? and C# one? and what parameters need DllImport for this?
What I've found to work best is to be more explicit about what's going on here. Having a string as return type is probably not recommended in this situation.
A common approach is to have the C++ side be passed the buffer and buffer size. If it's not big enough for what GetString has to put in it, the bufferSize variable is modified to indicate what an appropriate size would be. The calling program (C#) would then increase the size of the buffer to the appropriate size.
If this is your exported dll function (C++):
extern "C" __declspec void GetString( char* buffer, int* bufferSize );
Matching C# would be the following:
void GetString( StringBuilder buffer, ref int bufferSize );
So to use this in C# you would then do something like the following:
int bufferSize = 512;
StringBuilder buffer = new StringBuilder( bufferSize );
GetString( buffer, ref bufferSize );
The only good way that I know of doing this is to write a .NET C++ wrapper class using Managed C++ Extensions, and within the .NET C++ object call your native C++ code. There are functions in the managed extensions to convert a System.String to a char* or any other type of unmanaged string.
Basically you create a .NET class using C++ and expose it from an assembly, and internally to that assembly you can call your native C++ code. The other way is to add a pure C function to your C++ code using P/Invoke and then call your C code from C# and have your C function call your C++ code. This will work, but I tend to try to use managed code as much as possible.
The biggest problem with passing strings from C++ back to C# is the memory allocation. The GC should be able to know how to cleanup the memory allocated for this string. Since C# has extensive COm interop support, it does know about COM BSTRs and how to allocate and deallocate these. Thus the easiest way to do this would be to use BSTR on the C++ side and string on the C# side.
Note, using BSTRs does not imply that your function has to be expose through COM.
The "string" return value is the problem. The P/Invoke marshaller is going to call CoTaskMemFree() on the pointer you return. That's not going to work well unless you used CoTaskMemAlloc() in your C/C++ code to allocate the string buffer. Which is a fairly unusual thing to do.
The best solution is to allow the caller of your code to pass a pointer to a buffer and the buffer length to you as arguments. That way all memory allocation happens on one side. Scott showed you how to do this.
I had to convert a unicode C# string to a multibyte representation in order to convert to char* in c++ (this is partial one way solution)
I found the following very useful
string st;
IntPtr stPtr = Marshal.StringToHGlobalAnsi(st);
// Do your thing in C++
Marshal.FreeHGlobal(stPtr);
This may be inefficient and not in C# manner, I'm new to C#.
All the examples I can find using DLLImport to call C++ code from C# passes ints back and forth. I can get those examples working just fine. The method I need call takes two structs as its import parameters, and I'm not exactly clear how I can make this work.
Here's what I've got to work with:
I own the C++ code, so I can make any changes/additions to it that I need to.
A third party application is going to load my DLL on startup and expects the DLLExport to be defined a certain way, so i can't really change the method signature thats getting exported.
The C# app I'm building is going to be used as a wrapper so i can integrate this C++ piece into some of our other applications, which are all written in C#.
The C++ method signature I need to call looks like this
DllExport int Calculate (const MathInputStuctType *input,
MathOutputStructType *output, void **formulaStorage)
And MathInputStructType is defined as the following
typedef struct MathInputStuctTypeS {
int _setData;
double _data[(int) FieldSize];
int _setTdData;
} MathInputStuctType;
The MSDN topic Passing Structures has a good introduction to passing structures to unmanaged code. You'll also want to look at Marshaling Data with Platform Invoke, and Marshaling Arrays of Types.
From the declaration you posted, your C# code will look something like this:
[DllImport("mydll.dll")]
static extern int Calculate(ref MathInputStructType input,
ref MathOutputStructType output, ref IntPtr formulaStorage);
Depending on the structure of MathInputStructType and MathOutputStructType in C++, you are going to have to attribute those structure declarations as well so that they marshal correctly.
For the struct:
struct MathInputStuctType
{
int _setData;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = FieldSize)]
double[] _data;
int _setTdData;
}
You might want to look at this project on CodePlex, http://www.codeplex.com/clrinterop/Release/ProjectReleases.aspx?ReleaseId=14120. It should help you marshal the structures correctly.