【发布时间】:2012-12-18 19:26:29
【问题描述】:
我有以下 MASM 代码:
.386
.model flat, stdcall
option casemap :none
include \masm32\include\masm32rt.inc
.data
NewLine db 13, 10, 0
.code
LibMain proc instance:dword,reason:dword,unused:dword
mov eax, 1
ret
LibMain endp
PrintMess proc
print "Printed from assembly"
invoke StdOut, addr NewLine
ret
PrintMess endp
TestReturn proc number:dword
mov eax, number
ret
TestReturn endp
End LibMain
使用简单的 .def 文件:
LIBRARY MyLib
EXPORTS PrintMess
EXPORTS TestReturn
我正在从 C# 调用 PrintMess 和 TestReturn:
[DllImport("MyLib")]
static extern void PrintMess();
[DllImport("MyLib")]
static extern int TestReturn(int num);
static void Main(string[] args) {
Console.WriteLine("Printed from C#");
PrintMess();
int value = TestReturn(30);
Console.WriteLine("Returned: " + value);
Console.ReadKey(true);
}
我第一次运行它时,它在Console.ReadKey(true) 处暂停,我得到了预期的输出:
Printed from C#
Printed from assembly
Returned: 30
如果我随后在我的 C# 项目中进行更改,比如将 TestReturn(30) 更改为 TestReturn(50),那么它的行为会很奇怪。程序没有错误地终止并且不会在Console.ReadKey(true) 上暂停(似乎它甚至没有到达那一行),这是我的输出:
Printed from C#
Printed from assembly
我必须重建装配项目。具体来说,我必须重新构建,如果我进行另一个常规构建,程序会继续出现异常。当我重建时,输出和行为恢复正常并反映输出中的数字变化。我的猜测是 Build 和 Rebuild 之间的某些不同部分会破坏 DLL。
为什么我必须重建以及如何设置它以使我不必重建?
【问题讨论】:
-
+1 表示包含汇编语言的问题。顺便说一句,不知道。
-
会不会是抛出异常?您是否尝试过在调试时打开异常?
-
我正在 Visual Studio 中进行调试,到了这一点,我抛出了异常(做错事从程序集中返回了一个已修复的值),但现在没有抛出异常。跨度>
-
这段代码破坏了堆栈。您将需要了解调用约定。任何一本关于 x86 汇编的好书都有很好的介绍。
-
你是对的。我错了。
标签: c# visual-studio-2010 interop masm