473,324 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

passing std::string from visual studio 2005 to an C++ DLL generated by VC 6.0

I've looked through this thread and still have quetions.
Suppose In visual studio 2005, I write the following

#pragam managed
class ManagedWrapper
{
void CallUnmanagedMethod() // The unmanaged class /method is
imported from a C++ DLL generated by vc 6.0
{
std::string* inputString = new string();
inputString = "Text";
UnmanagedClass uc;
uc.Run(inputString); // signature uc.Run(string* input)
}
}

since std::string* is unmanaged. So it's a mix mode code. So
inputString unmanaged. Why can't I do something like that?
So in this case I can work without Marshal.StringToHGlobalAnsi if I
don't have to copy content from managed String, right?

Second question is,
I also tried this,
String^ text = "AAA";
char* buffer = (char*)Marshal.StringToHGlobalAnsi(text).ToPtr();
std::string input;
input = buffer;
UnmanagedClass uc; // imported from a C++ DLL generated by VC 6.0
uc.Run(input);

Now the questions is,
This piece of code compile and link perfectly. But when Í run it, my
unmanaged class just got some garbage string. If there are other
parameter after string,
those parameter will be filled with garbage data. But unmanaged DLL
doesn't complain. It just cast the input into the right type.
Why is it?
Thanks a lot if anyone can help me.

Oct 11 '07 #1
13 2851
Creativ wrote:
I've looked through this thread and still have quetions.
Suppose In visual studio 2005, I write the following

#pragam managed
class ManagedWrapper
{
void CallUnmanagedMethod() // The unmanaged class /method is
imported from a C++ DLL generated by vc 6.0
{
std::string* inputString = new string();
inputString = "Text";
UnmanagedClass uc;
uc.Run(inputString); // signature uc.Run(string* input)
}
}
Creativ:

First of all this code should not compile, because because inputString
is a string*, not a string. But why are you allocating it on the heap
anyway? Doesn't your code leak memory?

Second, in general it is not possible to mix .exe's and .dll's created
using different versions of the VC compiler. It definitely will not work
if you pass library objects across the boundary, because these objects
may have different layouts in the two cases (this is certainly the case
for std::string in VC6/VC8).

--
David Wilkinson
Visual C++ MVP
Oct 11 '07 #2

"David Wilkinson" <no******@effisols.comwrote in message
news:%2******************@TK2MSFTNGP06.phx.gbl...
Creativ wrote:
>I've looked through this thread and still have quetions.
Suppose In visual studio 2005, I write the following

#pragam managed
class ManagedWrapper
{
void CallUnmanagedMethod() // The unmanaged class /method is
imported from a C++ DLL generated by vc 6.0
{
std::string* inputString = new string();
inputString = "Text";
UnmanagedClass uc;
uc.Run(inputString); // signature uc.Run(string* input)
}
}

Creativ:

First of all this code should not compile, because because inputString is
a string*, not a string. But why are you allocating it on the heap anyway?
Doesn't your code leak memory?

Second, in general it is not possible to mix .exe's and .dll's created
using different versions of the VC compiler. It definitely will not work
if you pass library objects across the boundary, because these objects may
have different layouts in the two cases (this is certainly the case for
std::string in VC6/VC8).
In effect, this means that C++ classes can't be part of a library's public
interface. I think that STL classes specifically forbid being
dllexport/dllimport-ed.
>
--
David Wilkinson
Visual C++ MVP

Oct 11 '07 #3
In effect, this means that C++ classes can't be part of a library's public
interface.
Actually, they can, but you must use the exact same compiler.
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Oct 12 '07 #4
Second, in general it is not possible to mix .exe's and .dll's created
using different versions of the VC compiler.
You can, but you can cannot expose any C++ objects. Plain C is ok.
In fact, with plain C you can even mix and match compilers as you want
(gcc/watcom/vs/you_name_it)
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Oct 12 '07 #5
Mihai N. wrote:
>Second, in general it is not possible to mix .exe's and .dll's created
using different versions of the VC compiler.
You can, but you can cannot expose any C++ objects. Plain C is ok.
In fact, with plain C you can even mix and match compilers as you want
(gcc/watcom/vs/you_name_it)
Mihai:

I said "in general".

--
David Wilkinson
Visual C++ MVP
Oct 12 '07 #6

"Mihai N." <nm**************@yahoo.comwrote in message
news:Xn********************@207.46.248.16...
>In effect, this means that C++ classes can't be part of a library's
public
interface.
Actually, they can, but you must use the exact same compiler.
That's not really a "public" interface then, but internal to one specific
application, because it prevents generic reuse of the library.
>

--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email

Oct 15 '07 #7
That's not really a "public" interface then, but internal to one specific
application, because it prevents generic reuse of the library.
Depends on your definition of "public"

In my definition "public" is "visible to the outside world."
The fact that "the world" doesn't understand it is another thing.

A public Java or C# class is not usable from C++ (or C),
but this does not make it less public.
The main problem is that the decorations for C++ are not standard,
so each compiler does it's own thing. And then the memory layout of
a C++ object is not standard, so there is also compiler speciffic stuff.

Yes, it is a pitty that the C++ standard does not cover those
areas, but it is not MS fault.
And, as much as MS would like to keep compatibility, there is no
way to move forward. We all asked for better C++ standard compatibility.
Especially in the templates area. So fixing that I guess changed the
std::string layout. Yes, it is a not good. But what can you do?

This was a known problem from a long time: you want "generic (C++)
reuse of the library", you have wrap it in C and only expose C api,
or use sources (STL style).
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Oct 16 '07 #8

"Mihai N." <nm**************@yahoo.comwrote in message
news:Xn********************@207.46.248.16...
>That's not really a "public" interface then, but internal to one specific
application, because it prevents generic reuse of the library.

Depends on your definition of "public"

In my definition "public" is "visible to the outside world."
The fact that "the world" doesn't understand it is another thing.
There are all kinds of exports which are not part of a public interface.
The entire Nt* family of functions (I think they're in ntdll.dll) for
example.

[snip]
>
This was a known problem from a long time: you want "generic (C++)
reuse of the library", you have wrap it in C and only expose C api,
or use sources (STL style).
Well, exposing a pure interface also gives binary compatibility while
preserving OO style. But those are essentially the three options for
reusable C++ libraries.
>

--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email

Oct 16 '07 #9
Well, exposing a pure interface also gives binary compatibility while
preserving OO style.
I have never tried that one, sounds interesting, I might take a look.
But I think I can understand why that would work ...

--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Oct 17 '07 #10
Mihai N. wrote:
>Well, exposing a pure interface also gives binary compatibility while
preserving OO style.
I have never tried that one, sounds interesting, I might take a look.
But I think I can understand why that would work ...
Mihai:

It has to work, because that is what COM does.

--
David Wilkinson
Visual C++ MVP
Oct 17 '07 #11
On Oct 16, 6:29 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
>
Well, exposing a pure interface also gives binary compatibility while
preserving OO style. But those are essentially the three options for
reusable C++ libraries.
Except that the pure interface cannot use library objects in it's
definition (eg, you can't have a method that take a std::string as
parameter in your pure interface). This makes this approach not very
much better that a C-style interface, because you'are doomed to define
all types used in the interface - or use "compatible" types like the
ugly BSTR and it's awfull API.

Arnaud
Oct 17 '07 #12
>But I think I can understand why that would work ...
It has to work, because that is what COM does.
I believe you that it works. I was just find an
explanation why it does, and I think I did.
I don't accept that "it works because COM does,"
I would rather say "COM works because this works"

The reason that why it works is because a pure
interface object has certain memory layout.
(and was trying to figure out exactly what is so
special about a pure interface that allows it,
what that memory layout is and why)
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Oct 18 '07 #13
Mihai N. wrote:
>>But I think I can understand why that would work ...
>It has to work, because that is what COM does.

I believe you that it works. I was just find an
explanation why it does, and I think I did.
I don't accept that "it works because COM does,"
I would rather say "COM works because this works"

The reason that why it works is because a pure
interface object has certain memory layout.
(and was trying to figure out exactly what is so
special about a pure interface that allows it,
what that memory layout is and why)
Mihai:

Yes, and COM relies on this same memory layout, which is what allows it
to be compiler independent.

[This is the only thing I know about COM; I never actually use it.]

--
David Wilkinson
Visual C++ MVP
Oct 18 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Sims | last post by:
Hi, I have a function that looks something like .... void Function( std::string &oldval ) { int iSome_size = xxx; // get some size char *tmp = NULL; tmp = new char;
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
12
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!";...
4
by: Ioannis Vranos | last post by:
Will "STL .NET" of VS 2005 include a fully managed std::string? -- Ioannis Vranos
1
by: bor_kev | last post by:
Hi! I'd like to know how to transform (or convert) a System :: String to a std :: string under Visual Studio (C++) 2005 Beta. Sincerely, bor_kev *---------------------------------* Posted...
3
by: doubts | last post by:
Hi all, I am trying to convert my bulk of code from VC++ 6.0 to VC++.Net. when using std::string type variable, the application causes exception at one instance and does not cause an exception at...
2
by: =?Utf-8?B?QWJoaW1hbnl1IFNpcm9oaQ==?= | last post by:
Hi, I am using Visual C++ in Visual Studio 2005 to create a Managed Wrapper around some C++ LIBS. I've created some classes that contains a pointer to the LIB classes and everthing seems to...
25
by: Bala2508 | last post by:
Hi, I have a C++ application that extensively uses std::string and std::ostringstream in somewhat similar manner as below std::string msgHeader; msgHeader = "<"; msgHeader += a; msgHeader...
10
bajajv
by: bajajv | last post by:
Hi, I was trying to implement CString with std::string and std::wstring. class CString { public: std::string str; CString() {str = "ABCD";} //this works }: class CString {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.