代码之家  ›  专栏  ›  技术社区  ›  Dave

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

  •  2
  • Dave  · 技术社区  · 17 年前

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

    是否有人提供了一个可以从.NET调用的C++ DLL的最小代码片段?我得到一个 Unable to find entry point named XXX in DLL 在我的C++ 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
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   Greg Hewgill    17 年前

    尝试使用 __decspec(dllexport) 神奇的精灵在你的C++函数中尘埃。此声明设置了从dll中成功导出函数所需的一些内容。您可能还需要使用winapi或类似的工具:

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

    winapi设置了函数调用约定,使其适合于从vb.net等语言进行调用。

        2
  •  0
  •   On Freund    17 年前

    您可以尝试查看导出的函数(通过dumpbin或dependency walker)并查看名称是否已损坏。

        3
  •  0
  •   dav    17 年前

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

    MyCase.CPP #包括“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;
    }
    

    MyCase. DEF 库MyClass

    EXPORTS
    
    multiply @1
    

    STDAFX.CPP #包括“stdafx.h”

    STDAFX

    // 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_)
    
    推荐文章