创建供Excel& amp;使用的C ++ .dll C#(32/64位窗口) [英] Creating C++ .dll for use by Excel & C# (32/64bit Window)

查看:96
本文介绍了创建供Excel& amp;使用的C ++ .dll C#(32/64位窗口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我想开始使用Visual C ++ Express 2010建立.dll数学库。

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

我想将.dll用作两个库的引用 Excel VBA & C#应用程序。我希望它可以在 32bit / 64bit 窗口中使用。生成后,.dll也应该可以被其他PC使用。

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. 我正确地开始使用Win32 Project吗? (看来我无法在Express版本中使用 ATL / MFC

我是否需要在.dll 2组不同的VBA和用C#加载函数? (VBA需要使用__stdcall)

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

在其他PC上使用.dll的任何部署设置吗? (需要任何regsvr32 cmd进程吗?)

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

是否有使C ++ DLL导出类的初学者示例? (例如VBA / C#可以从该dll实例化一个新类)...

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

因为我不确定是否我可以实例化ClassA& VBA / C#中的ClassB,其c ++ .dll中的.header文件如下:

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 ++构建可导出的DLL文件COM类型或C样式的函数,并且使其可以在32位和64位环境中工作。 DLL文件是一种PE / COFF映像,并且PE每个文件仅支持一个ISA(与Macintosh可执行格式比较,该格式允许在同一映像文件中使用多个ISA)。您必须重新编译两次,一个用于x86,另一个用于x64。唯一的例外是.NET AnyCPU DLL,但这是另一回事。

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.

根据此比较表,C ++ Express 2010不包括x64编译器,也不包括MFC和ATL库,尽管您可以将它们作为Windows SDK的一部分单独下载。

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.

针对您的问题:


  1. 我个人将从 Empty Project模板开始-我不喜欢Win32模板提供的所有不必要的标题和(貌似)复杂的初始项目设置。然后,我自己将自己的.c / .h / .cpp文件添加到自己的代码中。

  2. 如果仅导出函数而不是COM类型,则不需要担心接口。只要您的函数正确导出(请参见此处: http://msdn.microsoft.com/zh-cn/library/ys435b3s%28v=vs.80%29.aspx ),则可以从.NET(包括C#和VB.NET)和VB6调用它们/ VBA和C / C ++。

  3. 其他PC?

  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"?

这篇关于创建供Excel& amp;使用的C ++ .dll C#(32/64位窗口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆