Click here to Skip to main content
15,894,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm using extern keyword to use function in C++ unmanaged DLL
one of the function parameters is const unsigned char *var,
I tried to pass that parameter as a string , Byte array but that was useless.

Would you please help me to find the correct data type to pass that parameter

C#
[DllImport("DMT.dll")]
        static extern int RequestData(int comm_type, int conn_num, int slave_addr, int func_code, const unsigned char *data, int datalen);


it's a library for ModBus communication and the data parameter is usually passed as string but what i get instead is the Hex code of each char.
Posted

1 solution

Instead of const unsigned char *data use string with System.Runtime.InteropServicesMarshalAsAttribute(UnmanagedType); use System.Runtime.InteropServices.LPStr as a parameter.

See:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.unmanagedtype.aspx[^].

Also, look at calling conventions for the function. Figure out what calling conventions are used by "DMT.DLL" (maybe Cdecl?) and use appropriate value for System.Runtime.InteropServices.DllImportAttribute. See:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.callingconvention.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900