Marshalling string in PInvoke

M

Mike

Hi,

I need to acces a string (wchar_t*) in a C++ (win32) DLL from within
the Compact Framework.
The C++ Syntax is like this:

extern "C" __declspec(dllexport) int SomeFunc(wchar_t
*someString[128])
{
*someString = _T("Enter some text...");
}

In the c# code I declare the function as

[DllImport("myTest.dll", EntryPoint = "SomeFunc")]
static extern int SomeFunc(StringBuilder someString);

and access it with

StringBuilder sb = new StringBuilder(128);
int result = SomeFunc(sb);

The only thing I get back are 2 hieroglyphs.
I tried passing in a string and a IntPtr as well, but nothing works.

The netcf-interop-logfile looks like this:

int SomeFunc(System.Text.StringBuilder);
int (I4_VAL) SomeFunc(WCHAR * (STRINGBUILDER_LPWSTR));

Can anyone give me a hint what I am doing wrong?

Thanks in advance.

Mike
 
C

Christian Resma Helle

Hi Mike,

Here's an example of how I would normally do it:


[C++ CODE]

extern "C" __declspec(dllexport) int TTN_GetApplicationVersion(
int* iError, LPTSTR szVersion, int *iBuildNumber )
{
MTomTomCommunicationLayerInterface *comms =
DEFAULT_TRANSPORTATION_LAYER(CLIENT_NAME,2005,TOMTOM_TCPIP_PORT);

CTomTomAPI api(*comms);
CTomTomAPI::TVersion version;
res = api.GetApplicationVersion(&err, &version);
*iError = err.iError;

TCHAR str[16];
_stprintf(str, TEXT("%S"), version.iVersion);
lstrcpy( szVersion, (LPTSTR)str );
*iBuildNumber = version.iBuildNumber;

delete comms;
return res;
}

[C# CODE]

[DllImport("TTSDK.dll", EntryPoint="TTN_GetApplicationVersion")]
static extern int TTN_GetApplicationVersion(
ref int iError, StringBuilder szVersion, ref int iBuildNumber);

public void TestGetApplicationVersion()
{
StringBuilder szVersion = new StringBuilder();
int iBuildNumber = 0;

TTN_GetApplicationVersion(ref iError, szVersion, ref iBuildNumber);
}


The code above is a wrapper to a C++ SDK to expose C style methods. Hope
this helps you.
 
M

Mike

Hi Mike,

Use LPTSTR instead of wchar_t *

--
Regards,
Christian Resma Hellehttp://christian-helle.blogspot.com

I need to acces a string (wchar_t*) in a C++ (win32) DLL from within
the Compact Framework.
The C++ Syntax is like this:
extern "C" __declspec(dllexport) int SomeFunc(wchar_t
*someString[128])
{
*someString = _T("Enter some text...");

In the c# code I declare the function as
[DllImport("myTest.dll", EntryPoint = "SomeFunc")]
static extern int SomeFunc(StringBuilder someString);
and access it with
StringBuilder sb = new StringBuilder(128);
int result = SomeFunc(sb);
The only thing I get back are 2 hieroglyphs.
I tried passing in a string and a IntPtr as well, but nothing works.
The netcf-interop-logfile looks like this:
int SomeFunc(System.Text.StringBuilder);
int (I4_VAL) SomeFunc(WCHAR * (STRINGBUILDER_LPWSTR));
Can anyone give me a hint what I am doing wrong?
Thanks in advance.
Mike- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Hi Christian,

thanks for the response. I tried it, but I get a similar result. There
is only returnd 00000000h: 3F 79
 
C

Christian Resma Helle

Hi Mike,

How does your native method look like?

--
Regards,
Christian Resma Helle
http://christian-helle.blogspot.com


Use LPTSTR instead of wchar_t *
Hi,
I need to acces a string (wchar_t*) in a C++ (win32) DLL from within
the Compact Framework.
The C++ Syntax is like this:
extern "C" __declspec(dllexport) int SomeFunc(wchar_t
*someString[128])
{
*someString = _T("Enter some text...");
}
In the c# code I declare the function as
[DllImport("myTest.dll", EntryPoint = "SomeFunc")]
static extern int SomeFunc(StringBuilder someString);
and access it with
StringBuilder sb = new StringBuilder(128);
int result = SomeFunc(sb);
The only thing I get back are 2 hieroglyphs.
I tried passing in a string and a IntPtr as well, but nothing works.
The netcf-interop-logfile looks like this:
int SomeFunc(System.Text.StringBuilder);
int (I4_VAL) SomeFunc(WCHAR * (STRINGBUILDER_LPWSTR));
Can anyone give me a hint what I am doing wrong?
Thanks in advance.
Mike- Zitierten Text ausblenden -
- Zitierten Text anzeigen -

Hi Christian,

thanks for the response. I tried it, but I get a similar result. There
is only returnd 00000000h: 3F 79- Hide quoted text -

- Show quoted text -
 
M

Mike

Hi Mike,

How does your native method look like?

--
Regards,
Christian Resma Hellehttp://christian-helle.blogspot.com

Hi Mike,
Use LPTSTR instead of wchar_t *
--
Regards,
Christian Resma Hellehttp://christian-helle.blogspot.com
Hi,
I need to acces a string (wchar_t*) in a C++ (win32) DLL from within
the Compact Framework.
The C++ Syntax is like this:
extern "C" __declspec(dllexport) int SomeFunc(wchar_t
*someString[128])
{
*someString = _T("Enter some text...");
}
In the c# code I declare the function as
[DllImport("myTest.dll", EntryPoint = "SomeFunc")]
static extern int SomeFunc(StringBuilder someString);
and access it with
StringBuilder sb = new StringBuilder(128);
int result = SomeFunc(sb);
The only thing I get back are 2 hieroglyphs.
I tried passing in a string and a IntPtr as well, but nothing works.
The netcf-interop-logfile looks like this:
int SomeFunc(System.Text.StringBuilder);
int (I4_VAL) SomeFunc(WCHAR * (STRINGBUILDER_LPWSTR));
Can anyone give me a hint what I am doing wrong?
Thanks in advance.
Mike- Zitierten Text ausblenden -
- Zitierten Text anzeigen -
Hi Christian,
thanks for the response. I tried it, but I get a similar result. There
is only returnd 00000000h: 3F 79- Hide quoted text -
- Show quoted text -- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Hi Christian,

I noticed in your example that you do not use the braces after the
LPSTR keyword. But if I remove them in my method I get a compile
error
C2440: '=' : cannot convert from 'const wchar_t [23]' to 'WCHAR' in
the line

*someString = _T("Enter some text...");

Your example helps me very much, because I'm also dealing with a
native sdk. But I also need to make Debug Output directly in my code.

I also noticed, that you do not initialize your StringBuilder with the
capazity:
StringBuilder szVersion = new StringBuilder();

Is that correct?

Thanl you very much.

Mike
 
G

Guest

Mike,
Your C++ API method uses an array of wide character strings.
This method would work if you declared your C++ method as

extern "C" __declspec(dllexport) int SomeFunction(LPSTR someString
{
wsprintf(someString, _T("Enter some text...");
}

Then you should be able to
 
M

Mike

Mike,
Your C++ API method uses an array of wide character strings.
This method would work if you declared your C++ method as

extern "C" __declspec(dllexport) int SomeFunction(LPSTR someString
{
wsprintf(someString, _T("Enter some text...");

}

Then you should be able to



Mike said:
I need to acces a string (wchar_t*) in a C++ (win32) DLL from within
the Compact Framework.
The C++ Syntax is like this:
extern "C" __declspec(dllexport) int SomeFunc(wchar_t
*someString[128])
{
*someString = _T("Enter some text...");
}
In the c# code I declare the function as
[DllImport("myTest.dll", EntryPoint = "SomeFunc")]
static extern int SomeFunc(StringBuilder someString);
and access it with
StringBuilder sb = new StringBuilder(128);
int result = SomeFunc(sb);
The only thing I get back are 2 hieroglyphs.
I tried passing in a string and a IntPtr as well, but nothing works.
The netcf-interop-logfile looks like this:
int SomeFunc(System.Text.StringBuilder);
int (I4_VAL) SomeFunc(WCHAR * (STRINGBUILDER_LPWSTR));
Can anyone give me a hint what I am doing wrong?
Thanks in advance.
Mike- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

It works! Thank you guys. You really made my day. I was working on
this for 2 days.

Mike
 
C

Christian Resma Helle

Anytime Mike!

I wrote a small article related to this topic before.

If you're interested, then here's a link to it:
http://christian-helle.blogspot.com/2007/06/integrating-with-tomtom-navigator.html

--
Regards,
Christian Resma Helle
http://christian-helle.blogspot.com


Mike,
Your C++ API method uses an array of wide character strings.
This method would work if you declared your C++ method as
extern "C" __declspec(dllexport) int SomeFunction(LPSTR someString
{
wsprintf(someString, _T("Enter some text...");

Then you should be able to
Mike said:
Hi,
I need to acces a string (wchar_t*) in a C++ (win32) DLL from within
the Compact Framework.
The C++ Syntax is like this:
extern "C" __declspec(dllexport) int SomeFunc(wchar_t
*someString[128])
{
*someString = _T("Enter some text...");
}
In the c# code I declare the function as
[DllImport("myTest.dll", EntryPoint = "SomeFunc")]
static extern int SomeFunc(StringBuilder someString);
and access it with
StringBuilder sb = new StringBuilder(128);
int result = SomeFunc(sb);
The only thing I get back are 2 hieroglyphs.
I tried passing in a string and a IntPtr as well, but nothing works.
The netcf-interop-logfile looks like this:
int SomeFunc(System.Text.StringBuilder);
int (I4_VAL) SomeFunc(WCHAR * (STRINGBUILDER_LPWSTR));
Can anyone give me a hint what I am doing wrong?
Thanks in advance.
Mike- Zitierten Text ausblenden -
- Zitierten Text anzeigen -

It works! Thank you guys. You really made my day. I was working on
this for 2 days.

Mike- Hide quoted text -

- Show quoted text -
 
M

Mike

Anytime Mike!

I wrote a small article related to this topic before.

If you're interested, then here's a link to it:http://christian-helle.blogspot.com/2007/06/integrating-with-tomtom-n...

--
Regards,
Christian Resma Hellehttp://christian-helle.blogspot.com

Mike,
Your C++ API method uses an array of wide character strings.
This method would work if you declared your C++ method as
extern "C" __declspec(dllexport) int SomeFunction(LPSTR someString
{
wsprintf(someString, _T("Enter some text...");
}
Then you should be able to
:
Hi,
I need to acces a string (wchar_t*) in a C++ (win32) DLL from within
the Compact Framework.
The C++ Syntax is like this:
extern "C" __declspec(dllexport) int SomeFunc(wchar_t
*someString[128])
{
*someString = _T("Enter some text...");
}
In the c# code I declare the function as
[DllImport("myTest.dll", EntryPoint = "SomeFunc")]
static extern int SomeFunc(StringBuilder someString);
and access it with
StringBuilder sb = new StringBuilder(128);
int result = SomeFunc(sb);
The only thing I get back are 2 hieroglyphs.
I tried passing in a string and a IntPtr as well, but nothing works.
The netcf-interop-logfile looks like this:
int SomeFunc(System.Text.StringBuilder);
int (I4_VAL) SomeFunc(WCHAR * (STRINGBUILDER_LPWSTR));
Can anyone give me a hint what I am doing wrong?
Thanks in advance.
Mike- Zitierten Text ausblenden -
- Zitierten Text anzeigen -
It works! Thank you guys. You really made my day. I was working on
this for 2 days.
Mike- Hide quoted text -
- Show quoted text -- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Looks like a great article. I will have a depper look the next week.
The strings in C/C++ always kill me. I'm so glad we don't have to deal
with them like this in the dotnet framework.

Mike
 
G

Guest

Why? that's the same thing.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com


Christian Resma Helle said:
Hi Mike,

Use LPTSTR instead of wchar_t *

--
Regards,
Christian Resma Helle
http://christian-helle.blogspot.com

Hi,

I need to acces a string (wchar_t*) in a C++ (win32) DLL from within
the Compact Framework.
The C++ Syntax is like this:

extern "C" __declspec(dllexport) int SomeFunc(wchar_t
*someString[128])
{
*someString = _T("Enter some text...");

}

In the c# code I declare the function as

[DllImport("myTest.dll", EntryPoint = "SomeFunc")]
static extern int SomeFunc(StringBuilder someString);

and access it with

StringBuilder sb = new StringBuilder(128);
int result = SomeFunc(sb);

The only thing I get back are 2 hieroglyphs.
I tried passing in a string and a IntPtr as well, but nothing works.

The netcf-interop-logfile looks like this:

int SomeFunc(System.Text.StringBuilder);
int (I4_VAL) SomeFunc(WCHAR * (STRINGBUILDER_LPWSTR));

Can anyone give me a hint what I am doing wrong?

Thanks in advance.

Mike
 

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