在MFC类中调用结构 [英] Call structure in class of MFC

查看:58
本文介绍了在MFC类中调用结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MFC基于对话框的应用程序中,我将使用函数指针调用不同条件的dll函数,所以我认为这些函数指针在包含结构&的包装函数中声明。最后返回包含函数指针地址的结构指针。怎么做?任何想法?

In MFC dialogue based application,I've to call dll function using function pointer for different condition, so I think these function pointers are declared in wrapper function which containing structure & at last returns structure pointer which containing address of function pointer.How to do that? any idea?

void *UniqueFunction(int flag)
{
	if(flag==0)
	{
		struct MyStruct
		{
		typedef int(*pFunctionPointer1)(CString,HANDLE *);
		typedef int(*pFunctionPointer2)(int,HANDLE);
		typedef int(*pFunctionPointer3)(int,int,char*,HANDLE);
		typedef int(*pFunctionPointer4)(int,int,int,HANDLE);
		typedef int(*pFunctionPointer5)(HANDLE);
		typedef CString(*pFunctionPointer6)(DWORD);
		}*FirstStruct;
		return FirstStruct;
	}



在上面的代码中,我试图在函数中声明结构,它编译错误但是如何在点击按钮的事件上调用它?这是对还是错?请建议!!


In above code I've tried to declare structure in function,it compiled error free but how to call that on event of button clicked?and is this right or wrong?Please suggest!!

推荐答案

您只是将一个未初始化的void指针返回给调用函数。这有用吗?

我想你必须让结构声明对调用者可用(它不应该是本地的)你必须以某种方式初始化(​​你的要求对我来说不明确)指针你正在返回。
You are just returning a uninitialized void pointer to the calling function. Is that useful?
I suppose you have to make the struct declaration available to the caller (that it should not be local) and you have to initialize somehow (your requirements are not clear to me) the pointer you are returning.


你需要实例化来自 typedefs 的实际指针。你的结构毫无意义,因为它只包含类型的定义,但没有实际的变量。你需要这样的东西:

You need to instantiate the actual pointers from the typedefs. Your structure makes no sense as it only contains the definitions of the types but no actual variables. You need something like:
struct MyStruct
{
    typedef int(*pFunctionPointer1Type)(CString,HANDLE *);
    pFunctionPointer1Type pFunctionPointer1;
//    typedef int(*pFunctionPointer2)(int,HANDLE);
//    typedef int(*pFunctionPointer3)(int,int,char*,HANDLE);
//    typedef int(*pFunctionPointer4)(int,int,int,HANDLE);
//    typedef int(*pFunctionPointer5)(HANDLE);
//    typedef CString(*pFunctionPointer6)(DWORD);
}*FirstStruct;
    FirstStruct = new MyStruct;
    FirstStruct->pFunctionPointer1 = FunctionOne;
// repeat for other pointers

// or if using dynamic DLL
HINSTANCE hDLL = ::LoadLibrary("mydllname");
FirstStruct->pFunctionPointer1 = (pFunctionPointer1Type)::GetProcAddress(hDLL, "FunctionOne");

return FirstStruct;




我想你必须阅读MSDN中的GetProcaddress函数。

参见这篇文章:

http://stackoverflow.com/questions/6031431/getprocaddress-function-in-c [ ^ ]



我通常做的是:

定义不同函数的typedef

声明一个包含所有类型函数的结构或类

调用loadLibrary

然后使用GetProcAddress获取库的每个函数的指针



之后你可以调用struct.function1(参数)。



祝你好运。
Hi,
I think you have to read about GetProcaddress function in MSDN.
see this article:
http://stackoverflow.com/questions/6031431/getprocaddress-function-in-c[^]

What I do usually is:
define typedef of different functions
declare a structure or a class with all types of functions
call loadLibrary
then get the pointer of each function of the library using GetProcAddress

after them you can call struct.function1(parameter).

Best regards.


这篇关于在MFC类中调用结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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