Creating C++ .dll for use by Excel & C# (32/64bit

2019-03-02 18:12发布

Recently I would like to start building a .dll maths library with Visual C++ Express 2010.

I would like to use the .dll as library reference for BOTH Excel VBA & C# applications. I expect it will work in both 32bit/64bit Window. The .dll should also be able to be used by other PC after build.

  1. Am I correct to start by using Win32 Project? (it seems I cannot use ATL / MFC in Express version)

  2. Do I need to specify in the .dll 2 different sets of interface for VBA & C# to load functions? (VBA require using __stdcall)

  3. Any deployment settings for using the .dll in other PC? (need any regsvr32 cmd process?)

  4. Any beginner example for making a C++ DLL to export a class? (e.g. VBA / C# can instantiate a new class from this dll) ...

Because I am not sure if I can instantiate ClassA & ClassB in VBA / C# with below .header file in the c++ .dll:

#pragma once

#ifdef DLLDIR_EX
    #define DLLDIR  __declspec(dllexport)   // export DLL information
#else
   #define DLLDIR  __declspec(dllimport)   // import DLL information
#endif 

class DLLDIR ClassA
{
public:
   void AMethod1();
   void AMethod2();
};

class DLLDIR ClassB
{
public:
   void BMethodi();
   void BMethodii();
};

标签: c# c++ winapi vba dll
2条回答
狗以群分
2楼-- · 2019-03-02 18:39

You cannot use C++ to build a DLL file that exports COM types or C-style functions and have it work in both a 32-bit and 64-bit environment. DLL files are a kind of PE/COFF image, and PE only supports one ISA per file (compare to the Macintosh executable format which allows for multiple ISAs in the same image file). You have to recompile twice, one for x86, the other for x64. The only exception to this rule are .NET "AnyCPU" DLLs, but that's another story.

According to this comparison table, C++ Express 2010 does not include the x64 compiler, nor does it include the MFC and ATL libraries, though you can download these as part of the Windows SDK separately.

In response to your questions:

  1. Personally I'd start off with the "Empty Project" template - I don't like all the unnecessary headers and (seemingly) complicated initial project settings that the Win32 template provides. I would then manually add my own .c/.h/.cpp files myself and code-away.
  2. If you're just exporting functions rather than COM types then you don't need to worry about interfaces. As long as your functions are exported correctly (see here: http://msdn.microsoft.com/en-us/library/ys435b3s%28v=vs.80%29.aspx ) then they are callable from .NET (incl C# and VB.NET) and VB6/VBA, and C/C++.
  3. "other PC"?
查看更多
Animai°情兽
3楼-- · 2019-03-02 18:49

About #2, you don't need two sets of interfaces because C# can also use the __stdcall calling convention.

查看更多
登录 后发表回答