|
|
|
start date: Fri, 13 Jul 2007 03:25:29 -0700,
posted on: microsoft.public.dotnet.framework.compactframework
back
| Thread Index |
|
1
Mike
|
|
2
Christian Resma Helle
|
|
3
ctacke/ ctacke[at]opennetcf[dot]com
|
|
4
Christian Resma Helle
|
|
5
Mike
|
|
6
Christian Resma Helle
|
|
7
Mike
|
|
8
dbgrick
|
|
9
Mike
|
|
10
Christian Resma Helle
|
|
11
Mike
|
Marshalling string in PInvoke
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
Date:Fri, 13 Jul 2007 03:25:29 -0700
Author:
|
Re: Marshalling string in PInvoke
Hi Mike,
Use LPTSTR instead of wchar_t *
--
Regards,
Christian Resma Helle
http://christian-helle.blogspot.com
On Jul 13, 12:25 pm, Mike wrote:
> 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
Date:Fri, 13 Jul 2007 03:59:13 -0700
Author:
|
Re: Marshalling string in PInvoke
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.
--
Regards,
Christian Resma Helle
http://christian-helle.blogspot.com
"Mike" wrote in message
news:1184322329.190038.310670@n2g2000hse.googlegroups.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
>
Date:Fri, 13 Jul 2007 13:13:29 +0200
Author:
|
Re: Marshalling string in PInvoke
On 13 Jul., 12:59, Christian Resma Helle wrote:
> Hi Mike,
>
> Use LPTSTR instead of wchar_t *
>
> --
> Regards,
> Christian Resma Hellehttp://christian-helle.blogspot.com
>
> On Jul 13, 12:25 pm, Mike wrote:
>
>
>
> > 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
Date:Fri, 13 Jul 2007 04:16:15 -0700
Author:
|
Re: Marshalling string in PInvoke
Hi Mike,
How does your native method look like?
--
Regards,
Christian Resma Helle
http://christian-helle.blogspot.com
On Jul 13, 1:16 pm, Mike wrote:
> On 13 Jul., 12:59, Christian Resma Helle wrote:
>
>
>
>
>
> > Hi Mike,
>
> > Use LPTSTR instead of wchar_t *
>
> > --
> > Regards,
> > Christian Resma Hellehttp://christian-helle.blogspot.com
>
> > On Jul 13, 12:25 pm, Mike wrote:
>
> > > 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 -
Date:Fri, 13 Jul 2007 04:22:27 -0700
Author:
|
Re: Marshalling string in PInvoke
On 13 Jul., 13:22, Christian Resma Helle wrote:
> Hi Mike,
>
> How does your native method look like?
>
> --
> Regards,
> Christian Resma Hellehttp://christian-helle.blogspot.com
>
> On Jul 13, 1:16 pm, Mike wrote:
>
>
>
> > On 13 Jul., 12:59, Christian Resma Helle wrote:
>
> > > Hi Mike,
>
> > > Use LPTSTR instead of wchar_t *
>
> > > --
> > > Regards,
> > > Christian Resma Hellehttp://christian-helle.blogspot.com
>
> > > On Jul 13, 12:25 pm, Mike wrote:
>
> > > > 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
Date:Fri, 13 Jul 2007 05:58:01 -0700
Author:
|
RE: Marshalling string in PInvoke
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" wrote:
> 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
>
>
Date:Fri, 13 Jul 2007 06:04:03 -0700
Author:
|
Re: Marshalling string in PInvoke
On 13 Jul., 15:04, dbgrick wrote:
> 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" wrote:
> > 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
Date:Fri, 13 Jul 2007 06:22:24 -0700
Author:
|
Re: Marshalling string in PInvoke
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
On Jul 13, 3:22 pm, Mike wrote:
> On 13 Jul., 15:04, dbgrick wrote:
>
>
>
>
>
> > 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" wrote:
> > > 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 -
Date:Fri, 13 Jul 2007 06:34:06 -0700
Author:
|
Re: Marshalling string in PInvoke
On 13 Jul., 15:34, Christian Resma Helle wrote:
> 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
>
> On Jul 13, 3:22 pm, Mike wrote:
>
>
>
> > On 13 Jul., 15:04, dbgrick wrote:
>
> > > 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" wrote:
> > > > 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
Date:Fri, 13 Jul 2007 07:12:42 -0700
Author:
|
Re: Marshalling string in PInvoke
Why? that's the same thing.
--
Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com
"Christian Resma Helle" wrote in message
news:1184324353.261271.123860@q75g2000hsh.googlegroups.com...
> Hi Mike,
>
> Use LPTSTR instead of wchar_t *
>
> --
> Regards,
> Christian Resma Helle
> http://christian-helle.blogspot.com
>
> On Jul 13, 12:25 pm, Mike wrote:
>> 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
>
>
Date:Fri, 13 Jul 2007 12:41:02 -0500
Author:
|
|
|