首页 / 知识

可以从.net调用的非常简单的C ++ DLL

2023-04-13 21:43:00

可以从.net调用的非常简单的C ++ DLL

Very simple C++ DLL that can be called from .net

我正在尝试从vb.net 2005调用第三方供应商的C DLL,并且出现P/Invoke错误。 我成功调用了其他方法,但是遇到了其中一种比较复杂的瓶颈。 所涉及的结构太可怕了,为了简化故障排除过程,我想创建一个C ++ DLL来复制问题。

有人可以为可以从.Net调用的C ++ DLL提供最小的代码段吗? 我的C ++ dll中出现Unable to find entry point named XXX in DLL错误。 解决起来应该很简单,但是我不是C ++程序员。

我想对.NET的DLL使用.net声明

1
Declare Function Multiply Lib"C:\\MyDll\\Debug\\MyDLL.DLL" Alias"Multiply" (ByVal ParOne As Integer, ByVal byvalParTwo As Integer) As Integer


尝试在C ++函数声明中使用__decspec(dllexport)魔术像素尘。 该声明设置了成功地从DLL导出函数所需的几件事。 您可能还需要使用WINAPI或类似的东西:

1
2
3
4
__declspec(dllexport) WINAPI int Multiply(int p1, int p2)
{
    return p1 * p2;
}

WINAPI设置了函数调用约定,以使其适合从VB.NET等语言进行调用。


根据格雷格的建议,我发现了以下作品。 如前所述,我不是C ++程序员,只是需要一些实用的东西。

myclass.cpp
#include" stdafx.h"

1
2
3
4
5
6
7
8
9
10
11
12
BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                 )
{
    return TRUE;
}

int _stdcall multiply(int x , int y)
{
    return x*y;
}

myclass.def
图书馆myclass

1
2
3
EXPORTS

multiply @1

stdafx.cpp
#include" stdafx.h"

stdafx.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__5DB9057C_BAE6_48D8_8E38_464F6CB80026__INCLUDED_)
#define AFX_STDAFX_H__5DB9057C_BAE6_48D8_8E38_464F6CB80026__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


// Insert your headers here
#define WIN32_LEAN_AND_MEAN     // Exclude rarely-used stuff from Windows headers

#include <windows.h>


//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__5DB9057C_BAE6_48D8_8E38_464F6CB80026__INCLUDED_)

您可以尝试查看导出的函数(通过DumpBin或Dependency Walker),并查看名称是否乱码。


调用第三方方法错误

最新内容

相关内容

热门文章

推荐文章

标签云

猜你喜欢