【问题标题】:Very simple C++ DLL that can be called from .net可以从 .net 调用的非常简单的 C++ DLL
【发布时间】:2010-09-07 12:47:14
【问题描述】:

我正在尝试从 vb.net 2005 调用第 3 方供应商的 C DLL,并收到 P/Invoke 错误。我成功地调用了其他方法,但在其中一个更复杂的方法上遇到了瓶颈。所涉及的结构非常可怕,为了简化故障排除,我想创建一个 C++ DLL 来复制问题。

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

我想为 DLL 使用 .net 声明

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

【问题讨论】:

    标签: .net c++ dll


    【解决方案1】:

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

    myclass.cpp #include "stdafx.h"

    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 图书馆我的班级

    EXPORTS
    
    multiply @1
    

    stdafx.cpp #include "stdafx.h"

    stdafx.h

    // 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_)
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        尝试在您的 C++ 函数声明中使用 __decspec(dllexport) 魔法粉。此声明设置了从 DLL 中成功导出函数所需的几项内容。您可能还需要使用 WINAPI 或类似的东西:

        __declspec(dllexport) WINAPI int Multiply(int p1, int p2)
        {
            return p1 * p2;
        }
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-03-04
          • 1970-01-01
          • 2011-05-22
          • 1970-01-01
          • 2011-02-20
          • 2012-09-30
          相关资源
          最近更新 更多