zack
zack

Reputation: 7365

Marshaling from C# to C++

I am new to C# and visual studio. I have a C# GUI that passes parameters to functions exported from a C++ DLL. The platform in Visual Studio 2005.

I have a function in the c++ DLL that take parameters of the following types: UINT8 UINT16 LPCWSTR someword (the following has been defined in the c+= dll : typedef void* someword..so basically someword is just a void pointer.)

Can you please help me how do i pass parameters from my C# GUI to this function imported from the DLL. I know it has to be done using MarshalAs but i donno how. also the c++ dll is unmanaged. Any help would be apreciated.

Thanks, Viren

Upvotes: 1

Views: 2196

Answers (2)

Marcin Deptuła
Marcin Deptuła

Reputation: 11957

Try something like this:

[DllImport("lib.dll")]
public static extern int FunctionName(IntPtr somevariable, ushort var1, byte var2, byte var3, byte var4, [MarshalAs(UnmanagedType.LPWStr), In] string str1, [MarshalAs(UnmanagedType.LPWStr), In] string str2);

Upvotes: 0

JaredPar
JaredPar

Reputation: 754753

Could you help us out by providing the signature of the C++ DLL? For the types you specified, here are the correct types

  • UINT8: UInt8
  • UINT16: UInt16
  • LPCWSTR: String (make sure to use the [In] Marshal attribute on the parameter as well)

Have you checked out the PInvoke Interop Assistant yet? It was designed to help people through these scenarios by converting C++ signatures into the equivalent DLL import

Upvotes: 1

Related Questions