Trouble calling cpp dll function whose input is char** [modified]
-
Hello, I am Developing a C# WINDOWS application. In my Application i need to call a cpp dll which returns the list of Drives connected to the PC. The Function is "int GetDeviceDiskName(char**);". I can get all the drive names in my vc++ application after i call this function. char *device[10]; for(int i = 0; i < 10; i++) { device[i] = new char[10]; memset(device[i],0,10); } int nRes = GetDeviceDiskName(device); device returns all the USB's attached to the PC. But I have Trobule calling this function in C#.Net. In my c# code i have declared the function in this manner, [DllImport("../../Hanlin.dll", SetLastError = true, CharSet = CharSet.Ansi,CallingConvention = CallingConvention.Cdecl)] public static extern Int32 GetDeviceDiskName(String[] deviceName); In c# i tried to call this function in this way, char[] deviceName = new char[10]; int nStatus = GetHanlinDeviceDiskName(ref deviceName); But I get Error as "Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." Can some one help me how to call this function. In the Dll file the "device" array will be updated in this way, if(bDiskFound) { device[cnt][0]=cDiskID; cnt++; }
modified on Thursday, October 8, 2009 9:09 AM
-
Hello, I am Developing a C# WINDOWS application. In my Application i need to call a cpp dll which returns the list of Drives connected to the PC. The Function is "int GetDeviceDiskName(char**);". I can get all the drive names in my vc++ application after i call this function. char *device[10]; for(int i = 0; i < 10; i++) { device[i] = new char[10]; memset(device[i],0,10); } int nRes = GetDeviceDiskName(device); device returns all the USB's attached to the PC. But I have Trobule calling this function in C#.Net. In my c# code i have declared the function in this manner, [DllImport("../../Hanlin.dll", SetLastError = true, CharSet = CharSet.Ansi,CallingConvention = CallingConvention.Cdecl)] public static extern Int32 GetDeviceDiskName(String[] deviceName); In c# i tried to call this function in this way, char[] deviceName = new char[10]; int nStatus = GetHanlinDeviceDiskName(ref deviceName); But I get Error as "Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." Can some one help me how to call this function. In the Dll file the "device" array will be updated in this way, if(bDiskFound) { device[cnt][0]=cDiskID; cnt++; }
modified on Thursday, October 8, 2009 9:09 AM
Marshaling char** parameters isn't easy, and probably not worthwhile. I would recommend any of three alternatives: 1. implement the functionality directly in C# 2. call an existing native function of a lower level, e.g. dealing with just one device at a time (pass a StringBuilder to be filled) 3. pass a StringBuilder instance with sufficient capacity, and store the device names in there, separated by some character, say a semi-colon; upon return call ToString() and Split(';') on it. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!