简体   繁体   中英

C# Dllimport problems in Visual Studio 2010 .NET framework 4

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.

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.

Change the return type to StringBuilder :

[DllImport("FindCaptureDevice.dll")]
public static extern StringBuilder GetDeviceName(int index);

您可以使用WIA捕获或扫描图像

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM