P/Invoke : getting a string from a dll (returned or in param) ???

Y

Yannick S.

hi all,

I want to construct a string in a dll and then call this function in C#. I
have try the MS samples but none work for the compact framework.
in my C dll :
yannick.c :
extern "C" YANNICK_DLL_API char* TestStringAsResult()
{
char* result = new char[64];//(char*)CoTaskMemAlloc( 64 );
strcpy( result, "This is return value" );
return result;
}
yannick.h :
extern "C" SWI_WIFIFUNCTIONS_DLL_API char* TestStringAsResult();
in my C# :
[ DllImport( "yannick.dll" )]

public static extern String TestStringAsResult();


but when I call the TestStringAsResult() method I have an exception
NotSupportedException.



can you help retrieve a string from a dll to my c# app ?

thx in advance.
 
Y

Yannick S.

I succeed :)

extern "C" YANNICK_DLL_API void stringPointer(WCHAR *a)
{

a[0] = '3';
a[1] = '4';
a[3] = '4';
a[4] = '3';
a[5] = '4';
a[6] = '4';
a[7] = '3';
a[8] = '4';
a[9] = '4';
a[10] = '3';
a[11] = '4';
a[12] = '4';
}
[DllImport("SWI_WifiFunctions_Dll.dll", CharSet=CharSet.Auto)]

public static extern void stringPointer(string i);

string aa = new string(' ',100);

stringPointer(aa);
 
B

Brian Smith [MSFT]

While this may appear to work, it is dangerous and not advised. The String
class is designed to be immutable -- if you pass a String to a p/invoke it
should be considered "read-only". If you need to modify a String from with
a p/invoke, you should use a StringBuilder instead. If you search this
newsgroup you will many examples of this technique.

--------------------
From: "Yannick S." <y.schmieder_nospam@swinnovation_nospam.com>
References: <#[email protected]>
Subject: Re: P/Invoke : getting a string from a dll (returned or in param) ???
Date: Fri, 29 Oct 2004 10:55:05 +0200
I succeed :)

extern "C" YANNICK_DLL_API void stringPointer(WCHAR *a)
{

a[0] = '3';
a[1] = '4';
a[3] = '4';
a[4] = '3';
a[5] = '4';
a[6] = '4';
a[7] = '3';
a[8] = '4';
a[9] = '4';
a[10] = '3';
a[11] = '4';
a[12] = '4';
}
[DllImport("SWI_WifiFunctions_Dll.dll", CharSet=CharSet.Auto)]

public static extern void stringPointer(string i);

string aa = new string(' ',100);

stringPointer(aa);



Yannick S. said:
hi all,

I want to construct a string in a dll and then call this function in C#. I
have try the MS samples but none work for the compact framework.
in my C dll :
yannick.c :
extern "C" YANNICK_DLL_API char* TestStringAsResult()
{
char* result = new char[64];//(char*)CoTaskMemAlloc( 64 );
strcpy( result, "This is return value" );
return result;
}
yannick.h :
extern "C" SWI_WIFIFUNCTIONS_DLL_API char* TestStringAsResult();
in my C# :
[ DllImport( "yannick.dll" )]

public static extern String TestStringAsResult();


but when I call the TestStringAsResult() method I have an exception
NotSupportedException.



can you help retrieve a string from a dll to my c# app ?

thx in advance.

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Y

Yannick S.

should I use something like that ?
StringBuilder str = new StringBuilder();

int m =MyFunc3(str);

with MyFunc3(WCHAR* str){

for (int i =0; i<100;i++){ str = 'a';}

}

is this correct ?

Brian Smith said:
While this may appear to work, it is dangerous and not advised. The String
class is designed to be immutable -- if you pass a String to a p/invoke it
should be considered "read-only". If you need to modify a String from with
a p/invoke, you should use a StringBuilder instead. If you search this
newsgroup you will many examples of this technique.

--------------------
From: "Yannick S." <y.schmieder_nospam@swinnovation_nospam.com>
References: <#[email protected]>
Subject: Re: P/Invoke : getting a string from a dll (returned or in param) ???
Date: Fri, 29 Oct 2004 10:55:05 +0200
I succeed :)

extern "C" YANNICK_DLL_API void stringPointer(WCHAR *a)
{

a[0] = '3';
a[1] = '4';
a[3] = '4';
a[4] = '3';
a[5] = '4';
a[6] = '4';
a[7] = '3';
a[8] = '4';
a[9] = '4';
a[10] = '3';
a[11] = '4';
a[12] = '4';
}
[DllImport("SWI_WifiFunctions_Dll.dll", CharSet=CharSet.Auto)]

public static extern void stringPointer(string i);

string aa = new string(' ',100);

stringPointer(aa);



Yannick S. said:
hi all,

I want to construct a string in a dll and then call this function in C#. I
have try the MS samples but none work for the compact framework.
in my C dll :
yannick.c :
extern "C" YANNICK_DLL_API char* TestStringAsResult()
{
char* result = new char[64];//(char*)CoTaskMemAlloc( 64 );
strcpy( result, "This is return value" );
return result;
}
yannick.h :
extern "C" SWI_WIFIFUNCTIONS_DLL_API char* TestStringAsResult();
in my C# :
[ DllImport( "yannick.dll" )]

public static extern String TestStringAsResult();


but when I call the TestStringAsResult() method I have an exception
NotSupportedException.



can you help retrieve a string from a dll to my c# app ?

thx in advance.

This posting is provided "AS IS" with no warranties, and confers no
rights.
 
B

Brian Smith [MSFT]

You will need to set the capacity of the StringBuilder to make sure the
native code have enough room to write its string. The easiest way would be
using the constructor:

StringBuilder str = new StringBuilder(100);

--------------------
From: "Yannick S." <y.schmieder_nospam@swinnovation_nospam.com>
Subject: Re: P/Invoke : getting a string from a dll (returned or in param) ???
Date: Wed, 3 Nov 2004 10:45:29 +0100

should I use something like that ?
StringBuilder str = new StringBuilder();

int m =MyFunc3(str);

with MyFunc3(WCHAR* str){

for (int i =0; i<100;i++){ str = 'a';}

}

is this correct ?

"Brian Smith [MSFT]" <briansm@nospam_microsoft.com> a écrit dans le message
de news: (e-mail address removed)...
While this may appear to work, it is dangerous and not advised. The String
class is designed to be immutable -- if you pass a String to a p/invoke it
should be considered "read-only". If you need to modify a String from with
a p/invoke, you should use a StringBuilder instead. If you search this
newsgroup you will many examples of this technique.

--------------------
From: "Yannick S." <y.schmieder_nospam@swinnovation_nospam.com>
References: <#[email protected]>
Subject: Re: P/Invoke : getting a string from a dll (returned or in
param)
???
Date: Fri, 29 Oct 2004 10:55:05 +0200
I succeed :)

extern "C" YANNICK_DLL_API void stringPointer(WCHAR *a)
{

a[0] = '3';
a[1] = '4';
a[3] = '4';
a[4] = '3';
a[5] = '4';
a[6] = '4';
a[7] = '3';
a[8] = '4';
a[9] = '4';
a[10] = '3';
a[11] = '4';
a[12] = '4';
}
[DllImport("SWI_WifiFunctions_Dll.dll", CharSet=CharSet.Auto)]

public static extern void stringPointer(string i);

string aa = new string(' ',100);

stringPointer(aa);



"Yannick S." <y.schmieder_nospam@swinnovation_nospam.com> a écrit dans le
message de %[email protected]...
hi all,

I want to construct a string in a dll and then call this function in
C#.
I
have try the MS samples but none work for the compact framework.
in my C dll :
yannick.c :
extern "C" YANNICK_DLL_API char* TestStringAsResult()
{
char* result = new char[64];//(char*)CoTaskMemAlloc( 64 );
strcpy( result, "This is return value" );
return result;
}
yannick.h :
extern "C" SWI_WIFIFUNCTIONS_DLL_API char* TestStringAsResult();
in my C# :
[ DllImport( "yannick.dll" )]

public static extern String TestStringAsResult();


but when I call the TestStringAsResult() method I have an exception
NotSupportedException.



can you help retrieve a string from a dll to my c# app ?

thx in advance.

This posting is provided "AS IS" with no warranties, and confers no
rights.


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top