469,599 Members | 2,874 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,599 developers. It's quick & easy.

Interop - string from dll issue


I am trying to retrieve string data from a c++ dll:

extern "C" __declspec( dllexport ) const char * getbagstr();
extern "C" __declspec( dllexport ) const char * getbagstr() {
const char * buff = g.getbag()->getbagstr(true).c_str();
return buff;
}

VS 2005, C# declaration

[DllImport("Hotspots",EntryPoint="getbagstr",
CallingConvention=CallingConvention.StdCall)]
private static extern string getbagstr();
console output after calling function in C# is:

v►☺YYYYYYYYYYYYYZZZZZZZZZ

Where are the extra characters coming from at the beginning? I have tried
returning a BSTR but I had the same result. The data is correct, but only
after the junk at the beginning.

Any ideas on how to avoid this problem?

Thanks.
Sep 7 '06 #1
5 2988
Hi,

Are you sure that g.getbag()->getbagstr(true).c_str(); is working correctly
and not inserting it?
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jason" <no****@nospam.comwrote in message
news:YM********************@bt.com...
>
I am trying to retrieve string data from a c++ dll:

extern "C" __declspec( dllexport ) const char * getbagstr();
extern "C" __declspec( dllexport ) const char * getbagstr() {
const char * buff = g.getbag()->getbagstr(true).c_str();
return buff;
}

VS 2005, C# declaration

[DllImport("Hotspots",EntryPoint="getbagstr",
CallingConvention=CallingConvention.StdCall)]
private static extern string getbagstr();
console output after calling function in C# is:

v??YYYYYYYYYYYYYZZZZZZZZZ

Where are the extra characters coming from at the beginning? I have tried
returning a BSTR but I had the same result. The data is correct, but only
after the junk at the beginning.

Any ideas on how to avoid this problem?

Thanks.

Sep 7 '06 #2
On Thu, 7 Sep 2006 14:02:00 -0400, Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

Are you sure that g.getbag()->getbagstr(true).c_str(); is working correctly
and not inserting it?
--
Yes, I am very confident about this, I have retested in VC++ 6.0 with the
same interfaces and everythign works fine. Only calling function through
c# interop causes this problem.
Sep 8 '06 #3
This is one possibility...

"Jason" <no****@nospam.comwrote in message
news:YM********************@bt.com...
>
I am trying to retrieve string data from a c++ dll:

extern "C" __declspec( dllexport ) const char * getbagstr();
extern "C": defaults to __cdecl, I think.
[DllImport("Hotspots",EntryPoint="getbagstr",
CallingConvention=CallingConvention.StdCall)]
But here is __stdcall.
In any case, you should not rely on the default, you should explicitly tag
the calling convention with the appropriate keyword in both C++ and C#.
Sep 8 '06 #4
On Fri, 8 Sep 2006 17:16:33 -0500, Ben Voigt wrote:
This is one possibility...

"Jason" <no****@nospam.comwrote in message
news:YM********************@bt.com...
>>
I am trying to retrieve string data from a c++ dll:

extern "C" __declspec( dllexport ) const char * getbagstr();
extern "C": defaults to __cdecl, I think.
>[DllImport("Hotspots",EntryPoint="getbagstr",
CallingConvention=CallingConvention.StdCall)]
But here is __stdcall.
In any case, you should not rely on the default, you should explicitly tag
the calling convention with the appropriate keyword in both C++ and C#.
Tried __stdcall and __cdecl and neither made any difference. There is a
problem with declaring the function in the dll, take a look at this

extern "C" __declspec( dllexport ) char * getbagstr();
extern "C" __declspec( dllexport ) char * getbagstr() {
const char * buff = g.getbag()->getbagstr(true).c_str();
char * c = const_cast<char*>(buff);
return c;
}

I cannot prefix char * with the calling convention as it will not compile.
I have tried declaring it as const char * as well, the latest version above
is just another attempt. I have tried specifiying all possible calling
conventions from C# whatever the C function defaults to and it still does
not work. This garbage data is really malignant.
Sep 9 '06 #5

"Jason" <no****@nospam.comwrote in message
news:_N********************@bt.com...
On Fri, 8 Sep 2006 17:16:33 -0500, Ben Voigt wrote:
>This is one possibility...

"Jason" <no****@nospam.comwrote in message
news:YM********************@bt.com...
>>>
I am trying to retrieve string data from a c++ dll:

extern "C" __declspec( dllexport ) const char * getbagstr();
extern "C": defaults to __cdecl, I think.
>>[DllImport("Hotspots",EntryPoint="getbagstr",
CallingConvention=CallingConvention.StdCall)]
But here is __stdcall.
In any case, you should not rely on the default, you should explicitly
tag
the calling convention with the appropriate keyword in both C++ and C#.

Tried __stdcall and __cdecl and neither made any difference. There is a
problem with declaring the function in the dll, take a look at this

extern "C" __declspec( dllexport ) char * getbagstr();
extern "C" __declspec( dllexport ) char * getbagstr() {
const char * buff = g.getbag()->getbagstr(true).c_str();
char * c = const_cast<char*>(buff);
return c;
}

I cannot prefix char * with the calling convention as it will not compile.
You wouldn't prefix char* with the calling convention, it is not the return
type that is modified. Rather it would look like:

extern "C" __declspec( dllexport ) const char * __stdcall getbagstr();

Anyway, don't use const_cast.
I have tried declaring it as const char * as well, the latest version
above
is just another attempt. I have tried specifiying all possible calling
conventions from C# whatever the C function defaults to and it still does
not work. This garbage data is really malignant.

Sep 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Trokey | last post: by
6 posts views Thread by Sudha | last post: by
3 posts views Thread by Tamir Khason | last post: by
2 posts views Thread by Olaf | last post: by
8 posts views Thread by Rob Edwards | last post: by
2 posts views Thread by Richard Collette | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.