将std :: string从visual studio 2005传递给VC 6.0生成的C ++ DLL [英] passing std::string from visual studio 2005 to an C++ DLL generated by VC 6.0

查看:61
本文介绍了将std :: string从visual studio 2005传递给VC 6.0生成的C ++ DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了这个帖子但仍然有排队。

假设在Visual Studio 2005中,我写了以下内容


#pragam managed

类ManagedWrapper

{

void CallUnmanagedMethod()//非托管类/方法是从C ++导入的
由vc 6.0生成的DLL

{

std :: string * inputString = new string();

inputString =" Text" ;;

UnmanagedClass uc;

uc.Run(inputString); //签名uc.Run(字符串*输入)

}

}


因为std :: string *是非托管的。所以这是一个混合模式代码。所以

inputString unmanaged。为什么我不能做那样的事情?

所以在这种情况下我可以在没有Marshal.StringToHGlobalAnsi的情况下工作如果我

不必从托管中复制内容字符串,对吗?


第二个问题是,

我也尝试了这个,

String ^ text =" AAA" ;;

char * buffer =(char *)Marshal.StringToHGlobalAnsi(text).ToPtr();

std :: string input;

输入=缓冲区;

UnmanagedClass uc; //从VC 6.0生成的C ++ DLL导入

uc.Run(输入);


现在的问题是,

这段代码完美地编译和链接。但是当它运行它时,我的

非托管类只是得到了一些垃圾字符串。如果字符串后面还有其他的

参数,那么这些参数将填充垃圾数据。但是非托管DLL
并没有抱怨。它只是将输入转换为正确的类型。

为什么会这样?

非常感谢,如果有人可以帮助我的话。

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.

推荐答案

Creativ写道:
Creativ wrote:

我已经浏览了这个帖子但仍然有排队。

假设在visual studio 2005中,我写了以下内容


#pragam managed

class ManagedWrapper

{

void CallUnmanagedMethod()//非托管类/方法是从vc 6.0生成的C ++ DLL导入的


{

std :: string * inputString = new string();

inputString =" Text";

UnmanagedClass uc;

uc。运行(inputString); //签名uc.Run(字符串*输入)

}

}
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:


首先这个代码不应该编译,因为因为inputString

是一个字符串*,而不是一个字符串。但是你为什么要在堆上分配

呢?难道你的代码没有泄漏内存吗?


其次,一般情况下不可能混合.exe'和.dll'的创建

使用不同版本的VC编译器。如果你跨越边界传递库对象肯定不会工作

,因为在这两种情况下这些对象

可能有不同的布局(这肯定是这样的情况) >
for VC6 / VC8中的std :: string。


-

David Wilkinson

Visual C ++ MVP

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




" David Wilkinson" < no ****** @ effisols.com写信息

新闻:%2 ****************** @ TK2MSFTNGP06.phx。 gbl ...

"David Wilkinson" <no******@effisols.comwrote in message
news:%2******************@TK2MSFTNGP06.phx.gbl...

Creativ写道:
Creativ wrote:

>我已查看此主题并且仍然有问题。
假设在visual studio 2005中,我写了以下内容

#pragam managed
类ManagedWrapper
{
void CallUnmanagedMethod()//从vc 6.0生成的C ++ DLL导入非托管类/方法
{
std :: string * inputString = new string();
inputString =" Text" ;
UnmanagedClass uc;
uc.Run(inputString); //签名uc.Run(字符串*输入)
}
}
>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:


首先这个代码不应该编译,因为因为inputString是
a string *,而不是字符串。但是你为什么还要在堆上分配呢?

难道你的代码没有泄漏内存吗?


其次,一般来说不可能混合.exe'和.dll'使用不同版本的VC编译器创建了

。如果你跨越边界传递库对象肯定不会工作

,因为在这两种情况下这些对象可能会有不同的布局(对于
VC6 / VC8中的std :: string)。


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).



实际上,这意味着C ++类不能成为库的公共

接口的一部分。我认为STL课程特别禁止

dllexport / dllimport-ed。

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
>
--
David Wilkinson
Visual C++ MVP



实际上,这意味着C ++类不能成为库的公共
In effect, this means that C++ classes can''t be part of a library''s public

接口的一部分。
interface.



实际上,他们可以,但你必须使用完全相同的编译器。

-

Mihai Nita [Microsoft MVP,Windows - SDK]
http://www.mihai-nita。网

------------------------------------- -----

将_year_替换为_以获取真实的电子邮件

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


这篇关于将std :: string从visual studio 2005传递给VC 6.0生成的C ++ DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆