Jeff Roder

Jeff Roder

  • NA
  • 4
  • 0

Problem using string args when calling C++ dll function C#

Jul 28 2009 9:06 AM

Hello All,
 
Here's my situation.  I've got an unmanaged C++ DLL with a simple test function that I am currently working with.  It looks like this:
 
extern
"C" {
   __declspec
(dllexport)void test(string input) {

         cout <<
"Inside test()." << endl;
   }
};
 
And on the C# side I have the following:
 
using
System;
using System.Collections.Generic;
using
System.Text;
using
System.Runtime.InteropServices;
 

class
MdsCrypt
{
      [
DllImport("MdsCryptDLL")]
      public static extern void test(String test);

}
 

class
Program
{
   static void Main()
   {
         System.
Console.WriteLine("Hello...");
         MdsCrypt MdsCrypt;
         MdsCrypt.test("test");
         while (true) ;
     }
}
 

The problem is that during runtime I get feedback that says memory was correupted.  Sometimes I get an assertion failure that says
 
_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
 
I don't understand why I can't use String arguments!!  If I change the string parameter to int in the test() function, no problems at all. 
 
I could really some help, does anyone have any insight??  Thanks much in advance.
 

Answers (1)