为什么 C++/CLI 中的内联汇编会产生可怕的问题?

发布于 2024-09-15 10:05:27 字数 809 浏览 1 评论 0原文

我在 C++/CLI 中使用内联汇编。事实上,可怕的问题可能是我观察到的一个错误。 我将向量从一个函数调用传递到另一个函数调用。如果我在被调用函数中注释 _asm{...some assembly code here} 的整个代码片段,其中使用了从其他函数提供给它的向量,那么整个向量就没有问题并成功复制到被调用函数的参数以正确的方式。

但是如果 uncmment _asm{} 我的意思是如果我在被调用的函数中使用 _asm{} 补丁,它会损坏对象的整个向量,事实上整个对象会损坏,其中包含向量和其他字符串数据,并且它只显示对象内的每个数据,例如 npos=4294967295 。

它是什么?为什么会这样呢?是 CLI 造成问题还是我使用内联 asm 的方式错误?

请帮助我,因为我被困在这里。

             strParamType  = strReturnType;
             if(strParamType.find("IDispatch")!=string::npos)
             {
                 IDispatch* pIDispatch; 
                 _asm
                 {
                   mov  esi,esp 
                       lea  eax,[pIDispatch] 
                   push eax
             }
              }

在这里,如果我在 _asm{} 内根本不写任何内容,即使出现我描述的问题。

问候 乌斯曼

I am using Inline asm in C++/CLI. Horrible problem infact could be a bug I obsereved.
I passed vector from one function call to another. If I comment the whole code snippet of _asm{....some assembly code here} inside the called function where vector used which are provided to it from other function, then no problem whole vector gets fine and copied to argument of called function successfully in correct manner.

But If uncmment _asm{} I mean if I use _asm{} patch in called function , it corrupts the whole vectors of objects infact whole object corrupts which contains vectors and other strings data and it just shows for every data inside object like npos=4294967295.

What is it? Why this is so? Is CLI creating problem or I am using inline asm i wrong way?

Kindly help me out , as I am stuck here.

             strParamType  = strReturnType;
             if(strParamType.find("IDispatch")!=string::npos)
             {
                 IDispatch* pIDispatch; 
                 _asm
                 {
                   mov  esi,esp 
                       lea  eax,[pIDispatch] 
                   push eax
             }
              }

Here If I dont write anything at all inside _asm{} even then problem occurs which I described.

Regards
Usman

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女情怀诗 2024-09-22 10:05:28

从您提供的源代码中,我无法完全猜测您打算做什么,但我有一些观察结果:

  1. 特别是在 .NET 环境中使用 _asm 时,您需要遵守一组规则。例如,请参阅Microsoft 的建议
  2. 您的代码未初始化 pIDispatch。我不知道您的实际代码中接下来的内容,但汇编代码获取堆栈上指针的地址,并将其再次存储在堆栈中。这会带来麻烦,除非您的代码尝试初始化指针。但在 if 子句之后,指针将超出范围!
  3. 您的汇编代码更改了堆栈指针而不恢复它。当然,这会让你的进程崩溃。除非您真的知道自己在做什么(例如编写操作系统),否则不要这样做。

From the source you provide I cannot quite guess what you intend to do, but I'd have a few observations:

  1. Especially for using _asm in a .NET environment you need to adhere to a set of rules. Refer to, for instance, Microsoft's advice.
  2. Your code doesn't initialize pIDispatch. I don't know what follows in your real code, but the assembler code takes the address of the pointer on your stack, and stores it on stack again. That is calling for trouble, unless your code tries to initialize the pointer. But after the if-clause, the pointer will be out of scope!
  3. Your assembly code changes the stack pointer without restoring it. This will crash your process, sure thing. Do not do it unless you really know what you're doing (like writing an operating system).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文