kev*_*evp 0 c# c++ dll pinvoke
我一直在努力研究c ++中需要运行的c ++代码.我浏览了这个DLL教程,在我的c#app中使用它时遇到了麻烦.我将在下面发布所有代码.
我收到此PInvokeStackImbalance错误:'调用PInvoke函数'frmVideo :: Add'使堆栈失衡.这很可能是因为托管PInvoke签名与非托管目标签名不匹配.检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配.
凯文,一如既往地谢谢
#ifndef _DLL_TUTORIAL_H_
#define _DLL_TUTORIAL_H_
#include <iostream>
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif
extern "C"
{
DECLDIR int Add( int a, int b );
DECLDIR void Function( void );
}
#endif
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
#define DLL_EXPORT
#include "DLLTutorial.h"
extern "C"
{
DECLDIR int Add( int a, int b )
{
return( a + b );
}
DECLDIR void Function( void )
{
std::cout << "DLL Called!" << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
using System.Runtime.InteropServices;
[DllImport(@"C:\Users\kpenner\Desktop\DllTutorialProj.dll"]
public static extern int Add(int x, int y);
int x = 5;
int y = 10;
int z = Add(x, y);
Run Code Online (Sandbox Code Playgroud)
您的C++代码使用cdecl调用约定和C#代码默认值stdcall.这种不匹配解释了您看到的信息.
使界面的两边匹配:
[DllImport(@"...", CallingConvention=CallingConvention.Cdecl]
public static extern int Add(int x, int y);
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用stdcallC++导出:
DECLDIR __stdcall int Add( int a, int b );
Run Code Online (Sandbox Code Playgroud)
这取决于您选择这两个选项中的哪一个,但请确保您只更改界面的一侧而不是两者,原因显而易见!
| 归档时间: |
|
| 查看次数: |
735 次 |
| 最近记录: |