Reputation: 31
I am trying to import a dll written in C++, in my C# application. The application is supposed to get names of disponible webcams. It works well in .NET framework 3.5 (gets all names properly) but I have problems with getting names in framework 4. I get something like "„îş" instead.
Here are fragments of my code :
c++ dll :
TCHAR GetDeviceName(int index)
{
char name[255];
GetDeviceNameAux(index, name);
TCHAR retVal = (TCHAR)name;
return retVal;
}
c# application (that works for framework 3.5)
[DllImport("FindCaptureDevice.dll")]
public static extern string GetDeviceName(int index);
I have tried also :
[DllImport("FindCaptureDevice.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern string GetDeviceName(int index);
with different parameters for CallingConvention ans CharSet.
Thanks in advance for any help.
Upvotes: 1
Views: 5613
Reputation: 31
Ok. Thanks to you comments I managed to solve my problem. I used website
and I changed in c# code
[DllImport("FindCaptureDevice.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern void GetDeviceName(StringBuilder response, int index);
and I call it with
StringBuilder tempDevice = new StringBuilder(255);
GetDeviceName(tempDevice, DevCount);
and in c++ code I wrote
void GetDeviceName(char * outChr, int index)
{
GetDeviceNameAux(index, outChr);
}
and I copy the data to outChr with strcpy_s method.
Upvotes: 2
Reputation: 81700
Change the return type to StringBuilder
:
[DllImport("FindCaptureDevice.dll")]
public static extern StringBuilder GetDeviceName(int index);
Upvotes: 1