Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Trouble calling cpp dll function whose input is char** [modified]

Trouble calling cpp dll function whose input is char** [modified]

Scheduled Pinned Locked Moved C#
csharpc++helpdata-structuresperformance
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    platso_588
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • P platso_588

      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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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!


      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups