【发布时间】:2011-01-19 04:47:06
【问题描述】:
我编写了一个 C DLL 和一些 C# 代码来测试,包括这个 DLL 并从中执行函数。我对这个过程不太熟悉,每当从 C# 源代码调用我的 DLL 函数时,都会收到 PInvokeStackImbalance 异常。代码如下(我已经注释掉大部分代码来隔离这个问题):
C# 包含代码:
using System;
using System.Runtime.InteropServices;
using System.IO;
namespace TestConsoleGrids
{
class Program
{
[DllImport("LibNonthreaded.dll", EntryPoint = "process")]
public unsafe static extern void process( long high, long low);
static void Main(string[] args)
{
System.Console.WriteLine("Starting program for 3x3 grid");
process( (long)0, (long)0 );
System.Console.ReadKey();
}
}
}
C++ DLL 函数代码
extern "C" __declspec(dllexport) void process( long high, long low );
void process( long high, long low )
{
// All code commented out
}
Visual Studio 生成的 dllmain 代码(我不明白这个结构,所以我把它包括在内)
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
异常详情如下:
对 PInvoke 函数“TestConsoleGrids!TestConsoleGrids.Program::process”的调用使堆栈失衡。这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。检查 PInvoke 签名的调用约定和参数是否与目标非托管签名匹配。
【问题讨论】:
-
您不需要 pinvoke 签名上的 unsafe 关键字。 PInvoke 处理从 .net 到本机的编组
-
您是否已按照详细信息的要求进行操作?检查调用约定?检查 DllImportAttribute 类的文档,msdn.microsoft.com/en-us/library/…,它具有 CallingConvention 属性,尝试将其设置为 CallingConvention.Cdecl。 [DllImport(..., CallingConvention=Callingconvention.Cdecl)]
-
谢谢拉瑟!这正是问题所在。我一直在关注教程,但没有人提到这一点。
标签: c# windows dll pinvoke dllimport