奇怪的错误 [英] Strange Error

查看:59
本文介绍了奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我一直收到一个奇怪的错误,无法将其固定下来。消息是:


此应用程序已请求运行时以不寻常的

方式终止它。

请联系该应用程序支持团队获取更多信息。


但是我并没有故意要求运行时终止于

不寻常的方式。令我头疼的问题是:


wcscpy(data,MyCode.c_str());


其中定义了数据和MyCode as:


wchar_t data [256];

std :: wstring AuthorCode;


奇怪的是程序似乎在Visual

Studio中正常运行,但每当我尝试从

命令行运行它时都会生成错误消息。试试看......围绕有问题的线路阻挡

没有帮助 - 它没有抓住异常。我不知道是什么'

导致这个...


任何想法都表示赞赏!


谢谢!

Hi all,

I keep getting a strange error and can''t pin it down. The message is:

This application has requested the Runtime to terminate it in an unusual
way.
Please contact the application''s support team for more information.

However I''m not purposely requesting that the Runtime terminate in an
"unusual way." The line that is causing me headaches is:

wcscpy(data, MyCode.c_str());

Where data and MyCode are defined as:

wchar_t data[256];
std::wstring AuthorCode;

The strange thing is that the program appears to be running fine in Visual
Studio, but generates the error message whenever I try to run it from the
command line. Putting a try...catch block around the line in question
doesn''t help - it doesn''t catch the exception. I have no idea what''s
causing this...

Any ideas are appreciated!

Thanks!

推荐答案

只是猜测起点...


wcscpy是一个宽字符函数,它的第二个参数需要一个

宽字符串来复制。在你的例子中,你正在使用

c_str这不是一个宽字符函数 - 它返回一个8

位字符串。 wcscpy将尝试将每两个字节组合为单个

字符。在您的输入字符串中。如果你在这个字符串中有一个奇数个字符

,wcscpy会在它击中最后一个字符时尝试访问

字符串之外的内存,这可能会导致崩溃?


Andy


BTW


我无法访问非托管长途一个托管类

VC ++。NET


当我这样做时,变量的内容似乎被破坏了。如果我
逐字节访问相同的变量,我得到正确的值。

无论我将变量设置为什么,为<返回的值都是br />
long始终是相同的值。发生了什么......任何人都可以帮助我吗?


代码的简短版本如下:


// HEADER

命名空间MyProgram

{


#pragma unmanaged

__nogc class unmanagedClass

{

public:unmanagedClass(); // CONSTRUCTOR


public:union {

struct {

long unmanagedLong; // UNMANAGED VARIABLE

} myData;

struct {

char bytes [4];

} b_myData;

} myUnion;

};

#pragma managed

public __gc class managedClass

{

public:managedClass(); // CONSTRUCTOR

private:unmanagedClass __nogc * ptrUnmanagedClass; // PTR TO UNMANAGED

CLASS

public:System :: String * Get_unmanagedLong(); //获取方法

UNMANAGED VARIAHBLE

};

}


// CPP列表


//构造函数

MyProgram :: unmanagedClass :: unmanagedClass(){

unmanagedLong = 1536; // HEX#0600

}

MyProgram :: managedClass :: managedClass(){

ptrUnmanagedClass = new unmanagedClass();

}

System :: String * MyProgram :: managedClass :: Get_unmanagedLong(){

System :: String * result;


// NEXT返回#0600的正确值

result = System :: String :: Concat(


System :: String: :Format(" {0:x}",__ box(ptrUnmanagedC lass-> myUnion.b_myData.bytes [0])),


System :: String :: Format (" {0:x}",__ box(ptrUnmanagedC lass-> myUnion.b_myData.bytes [1])),

System :: String :: Format(" ; {0:x}",__ box(ptrUnmanagedC lass-> myUnion.b_myData.bytes [2])),

System :: String :: Format(" { 0:x}",__ box(ptrUnmanagedC lass-> myUnion.b_myData.bytes [3])));


// NEXT RETURNS#73CB6A62的不正确值
result = System :: String :: Format(" {0:x}",__ box(ptrUnm anagedClass-> myUnion.myData.unmanagedLong) );


返回(结果);

}

Just a guess for a starting point...

wcscpy is a wide character function, and its second parameter expects a
wide character string to copy from. In your example, you are using
c_str which is not a wide character function - it returns a string of 8
bit chars. wcscpy will attempt to combine every two bytes as a single
"character" in your input string. If you have an odd number of chars
in this string, wcscpy will attempt to access memory outside of the
string when it hits the last character, which may cause the crash?

Andy

B.T.W.

I''m having trouble accessing an unmanaged long from a managed class in
VC++.NET

When I do, the contents of the variable seem to be mangled. If I
access the same variable byte-by-byte, I get the correct value.
Regardless what I set the variable to, the value that is returned for a
long is always the same value. What''s going on...can anyone help me?

A short version of the code follows:

//HEADER
namespace MyProgram
{

#pragma unmanaged
__nogc class unmanagedClass
{
public:unmanagedClass(); //CONSTRUCTOR

public: union{
struct {
long unmanagedLong; //UNMANAGED VARIABLE
} myData;
struct {
char bytes[4];
} b_myData;
} myUnion;
};
#pragma managed
public __gc class managedClass
{
public:managedClass(); //CONSTRUCTOR
private: unmanagedClass __nogc *ptrUnmanagedClass; //PTR TO UNMANAGED
CLASS
public:System::String* Get_unmanagedLong(); //METHOD TO GET
UNMANAGED VARIAHBLE
};
}

//CPP LISTING

//CONSTRUCTORS
MyProgram::unmanagedClass::unmanagedClass(){
unmanagedLong=1536; //HEX #0600
}
MyProgram::managedClass::managedClass(){
ptrUnmanagedClass=new unmanagedClass();
}
System::String* MyProgram::managedClass::Get_unmanagedLong(){
System::String *result;

//NEXT RETURNS CORRECT VALUE OF #0600
result=System::String::Concat(

System::String::Format("{0:x}",__box(ptrUnmanagedC lass->myUnion.b_myData.bytes[0])),

System::String::Format("{0:x}",__box(ptrUnmanagedC lass->myUnion.b_myData.bytes[1])),

System::String::Format("{0:x}",__box(ptrUnmanagedC lass->myUnion.b_myData.bytes[2])),

System::String::Format("{0:x}",__box(ptrUnmanagedC lass->myUnion.b_myData.bytes[3])));

//NEXT RETURNS INCORRECT VALUE OF #73CB6A62
result=System::String::Format("{0:x}",__box(ptrUnm anagedClass->myUnion.myData.unmanagedLong));

return(result);
}


Mike C#写道:
Mike C# wrote:

但是我并没有故意要求运行时终止于

不寻常的方式。令我头疼的问题是:


wcscpy(data,MyCode.c_str());
However I''m not purposely requesting that the Runtime terminate in an
"unusual way." The line that is causing me headaches is:

wcscpy(data, MyCode.c_str());



我建议您使用wcscpy_s,它要求您提供目标字符串的

长度。

I''d suggest that you use wcscpy_s, which requires you to supply the
length of the destination string.


其中数据和MyCode定义为:


wchar_t data [256];

std :: wstring AuthorCode;
Where data and MyCode are defined as:

wchar_t data[256];
std::wstring AuthorCode;



我的猜测是你的AuthorCode超过256 - 我建议你

单步执行你的代码,或者cout the length在您调用wcscpy之前的AuthorCode

(再次,wcscpy_s是一个更好的选择)。

My guess would be that your AuthorCode is longer than 256--I suggest you
step through your code, alternatively, cout the length of AuthorCode
just before you call wcscpy (again, wcscpy_s is a better alternative).


奇怪的是该程序似乎在Visual

Studio中正常运行,但每当我尝试从

命令行运行它时都会生成错误消息。试试看......围绕有问题的线路阻挡

没有帮助 - 它没有抓住异常。我不知道是什么'

导致这个...
The strange thing is that the program appears to be running fine in Visual
Studio, but generates the error message whenever I try to run it from the
command line. Putting a try...catch block around the line in question
doesn''t help - it doesn''t catch the exception. I have no idea what''s
causing this...



是的,wcscpy只会践踏目标字符串的末尾

没有任何例外。尝试使用wcscpy_s,并检查ERANGE(找出

,看看AuthorCode的大小是否太小)。


干杯

Ray

Yeah, wcscpy will just trample the end of the destination string over
without any exception. Try using wcscpy_s, and check for ERANGE (to find
out whether the size is too small for AuthorCode).

Cheers
Ray


任何想法都表示赞赏!


谢谢!

Any ideas are appreciated!

Thanks!


Andy写道:
Andy wrote:

wcscpy是一个宽字符函数,它的第二个参数需要一个

要复制的宽字符串。在你的例子中,你正在使用

c_str这不是一个宽字符函数 - 它返回一个8

位字符串。
wcscpy is a wide character function, and its second parameter expects a
wide character string to copy from. In your example, you are using
c_str which is not a wide character function - it returns a string of 8
bit chars.



但是c_str()属于一个wstring,所以它返回一个const wchar_t *


< snip>

But that c_str() belong to a wstring, so it returns a const wchar_t*

<snip>


这篇关于奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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