Example 1: Passing from C# to C/C++
If the C/C++ function has the following signature (where the second parameter is the Unicode string to be passed to the C++ function):
HRESULT SetGpsData( HANDLE hClient, const char* szNmeaSentence );
Then the following wrapper functions can be used:
[DllImport("myDynamicLinkLib.dll")]
private static extern int SetGpsData(IntPtr hClient, IntPtr szNmeaSentence);
public static int SetGpsData(IntPtr hClient, string szNmeaSentence)
{
IntPtr pszNmeaSentence = IntPtr.Zero;
pszNmeaSentence = Marshal.StringToHGlobalUni(szNmeaSentence);
int hr = SetGpsData(hClient, pszNmeaSentence);
Marshal.FreeHGlobal(pszNmeaSentence);
return hr;
}
Inside the wrapper function, the Unicode string has to be copied to unmanaged memory using the Marshal class' StringToHGlobalUni method first and then passed as a pointer to the C/C++ function. Finally, free up the unmanaged memory after you are done with the FreeHGlobal method.
To use the public wrapper function, simply do the following:
IntPtr hClient = IntPtr.Zero;
string szNmea = "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47";
int ret = SetGpsData( hClient, szNmea);
Example 2: Passing from C/C++ to C#
If a C/C++ function has the following signature (where the second parameter is the Unicode string value to be returned by the function):
BOOL GetTheString( HRESULT hr, LPWSTR szName, INT maxChar );
Then the wrapper functions in C# can be written as:
[DllImport("myDynamicLinkLib.dll")]
private static extern bool GetTheString(int hr, IntPtr szName, int maxChar);
public static bool GetTheString(int hr, out string szName)
{
bool ret = false;
IntPtr pSzName = IntPtr.Zero;
pSzName = Marshal.AllocHGlobal(260);
szName = string.Empty;
if (hr != 0)
{
bool status = GetTheString(hr, pSzName, 260);
if (status)
{
szName = Marshal.PtrToStringUni(pSzName);
}
}
Marshal.FreeHGlobal(pSzName);
return ret;
}
To use this wrapper function, simply do the following:
IntPtr hClient = IntPtr.Zero;
string szFromDLL = string.Empty;
bool ret = GetTheString( hClient, out szFromDLL);