RE: Passing strings to C++ DLL

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Andy, a couple options:

1) Look at the MarshalAsAttribute which tells .NET how to translate its
unicode string between it and unmanaged code; the attribute is applied to the
argument, here's the example from MSDN:

void MyMethod( [MarshalAs(LPStr)] string s);

2) The other option is to write the wrapper in managed C++; if you go this
route your method calls will look something like this:

void MyMethod(String^ s)
{
IntPtr ptr = Marshal::StringToHGlobalAnsi(str);;

try
{
SDCERR err = GetCurrentConfig( (char*)ptr.ToPointer() );
}
finally
{
if (ptr.ToPointer()) Marshal::FreeHGlobal(ptr);
}
}

HTH - KH



"Andy Baker" wrote:

I am attempting to write a .NET wrapper for a C++ DLL file, but am having
problems with passing strings as parameters. How should I be writing my C#
function call when the C header file is definined as taking a char * as an
argument? For example the C++ header says
SDCERR GetCurrentConfig(DWORD *num, char *name);
I am using Uint for the *num parameter, which returns the correct value
but for *name, I always get back a string of 6 squares. I have tried using
string, StringBuilder and char arrays, and passing by ref and not, but
always get the same result.
Connected to this I am also having problems with passing structures for
C++ functions - the structures contain strings as well. Whenever I call a
function that requires a structure as a parameter, I always get a
NotSupportedException, does this just mean that I have defined my structure
incorrectly? Do I have to initailise the structure beforehand (have tried
this but still didn't work)?
I am using Visual Studio 2005, and if it makes a difference it is for a
Compact Framework device. Thanks in advance for any help.

Andy Baker




.



Relevant Pages

  • Urgent! Passing C# string into C++ char*
    ... I got a WCE DLL in C++ with an exported function that takes char* type. ... PocketPC environment by passing C# string into it(since there is no char* ... public static extern int copyStringstring ...
    (microsoft.public.dotnet.framework.interop)
  • Re: strtok causes Segmentation fault
    ... You are passing a string literal as the list parameter, ... to my old C book strtok is supposed to take a string for the first ... int createvarible(const int sock, char *varname, char *vartype, char ... Your appear to be passing the function a static buffer or literal string. ...
    (comp.lang.c)
  • Re: Character limit in notes field
    ... "Andy" wrote in message ... Is there a way> for me to see all of the notes in the field when I print my projects for> meetings? ... the way that I write my notes now is to put the newest info> at the end of the string. ... >>> I searched for posts in this discussion group and found a string of posts ...
    (microsoft.public.project)
  • Re: translate menu in resource file
    ... "Andy Smirala" wrote in message ... i want to have menu language of my program in two different language, ... how to tell in menu editor to use a string from string table in resource file. ...
    (microsoft.public.vb.general.discussion)
  • Re: Generate serial numbers
    ... > Embedding random numbers in the string will slow down crackers. ... >>> Andy, ...
    (borland.public.delphi.thirdpartytools.general)