从Windows Forms应用程序实例化类库中的类 [英] Instantiating a Class in a Class Library from a Windows Forms Application

查看:102
本文介绍了从Windows Forms应用程序实例化类库中的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++ Windows窗体应用程序.我希望该应用程序能够使用"C ++类库"(dll)文件中包含的各种自定义模块,这些模块中都具有相同的公用函数调用,但是会执行不同的操作.该dll也可能具有该dll使用的其他支持功能,但它们是私有的.这些私有功能将从一个dll更改为另一个dll.该程序将不知道有哪些可用的dll,它将扫描文件夹并加载在启动时找到的任何文件.

当将DLL作为主应用程序中的引用添加并实例化/使用此简化示例时,我有类库的概念正在工作:

C ++类库:编译为dll:

I have a C++ Windows Forms Application. I want this application to be able to use various custom modules contained in "C++ Class Library" (dll) files, that will all have the same public function calls in them but will do different things. The dll may also have other support functions that the dll uses, but are private. These private functions will change from one dll to another. The program will not know what dlls are available, it''ll scan a folder and load in whatever it finds on startup.

I have the class library concept working when adding the DLL as a reference in the main app... and instantiating/using it like this simplified example:

C++ Class Library: compiles to dll:

using namespace System;
public ref class Module_Test {
	public:
		Module_Test(void) { a = 500; }
	private:
		int a;
 
	public: int GetNumber() { return a; }
};


C ++ Windows窗体应用程序摘录:


C++ Windows Forms Application excerpt:

Module_Test md1;
MessageBox::Show(String::Format("{0}", md1.GetNumber()), "This will display the number 500", MessageBoxButtons::OK, MessageBoxIcon::Information);


因此,为了使其按预期工作,我认为我需要使用LoadLibrary()和GetProcAddress(),并且在LoadLibrary(L"Module_Test.dll")工作时...我不太确定从何而来那里.我似乎无法让GetProcAddress()找到GetNumber()测试函数,也不知道如何强制转换或实例化它以像上面通过引用那样使用整个类.

我已经用谷歌搜索了几乎所有我能想到的单词组合,没有运气.我找到了很多类似的示例,但是它们通常不适用,因为它们是不同类型的代码...非托管,较旧的c ++代码dll等.我找不到任何较新的示例我在这里使用的是Visual Studio 2010项目类型.


So, to make it work as intended, I believe that I need to use LoadLibrary() and GetProcAddress(), and while LoadLibrary(L"Module_Test.dll") works... I''m not quite sure where to go from there. I can''t seem to get GetProcAddress() to find the GetNumber() test function, nor do I know how to cast it or instantiate it to be able to use the whole class as I do above via the use of a reference.

I''ve googled just about every combination of words I can think of, no luck. I''ve found lots of simialr examples, but they''re often not applicable because they''re different kinds of code... unmanaged, older c++ code dll, etc. I can''t find any newer examples that work with with the Visual Studio 2010 project types I''m using here.

Thoughts?

推荐答案

在这种情况下,我们使用的行为类似于类工厂.
只需在dll中具有一个导出的(全局)函数(使用__declspec(dllexport)),然后让该函数返回Module_Test类的对象/指针.
您可以使用GetProcAddress获取此dll全局函数,一旦获取其地址,则调用它以获取Module_Test对象/指针.

使用此Module_Test对象/指针,可以按通常的方式调用它的函数.

希望这能回答您的问题.
In these cases, we use functions which behaves like class factories.

Just have a exported (global) function (using__declspec(dllexport) ) in your dll and let this function return object/pointer of Module_Test class.

You can use GetProcAddress to get this dll global function and once you get its address then invoke it to get Module_Test object/pointer.

With this Module_Test object/pointer, you can call it''s functions in the usual way.

Hope this answers your problem.


我使用反射功能解决了这个问题
I solved this using Reflection


这篇关于从Windows Forms应用程序实例化类库中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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