DLL返回自定义结构的数组
Txml.h 自定义了结构体 与对应的类
View Code
typedef enum {
CHOICE_STYLE_OTHER,
CHOICE_STYLE_TEXT,
CHOICE_STYLE_IMG
}ChoiceStyle;
typedef struct {
char CIID;
ChoiceStyle EnumChoiceStyle;
char ChoicePath[MAXPATH];
char ChoiceContent[MAXQCONTENT];
}ChoiceItem;
typedef struct {
char QID[MAXID];
char QContent[MAXQCONTENT];
char ContentAudio[MAXPATH];
ChoiceItem ChoiceItemA;
ChoiceItem ChoiceItemB;
ChoiceItem ChoiceItemC;
ChoiceItem ChoiceItemD;
char Answer[1];
}QuestionItem;
CHOICE_STYLE_OTHER,
CHOICE_STYLE_TEXT,
CHOICE_STYLE_IMG
}ChoiceStyle;
typedef struct {
char CIID;
ChoiceStyle EnumChoiceStyle;
char ChoicePath[MAXPATH];
char ChoiceContent[MAXQCONTENT];
}ChoiceItem;
typedef struct {
char QID[MAXID];
char QContent[MAXQCONTENT];
char ContentAudio[MAXPATH];
ChoiceItem ChoiceItemA;
ChoiceItem ChoiceItemB;
ChoiceItem ChoiceItemC;
ChoiceItem ChoiceItemD;
char Answer[1];
}QuestionItem;
class Txml
{
public:
Txml();
~Txml();
private:
QuestionItem QuestionItemArray[MAXARRAYSIZE];
int QuestionItemArraySize;
public:
int LoadXml(const char *XmlFilePath);
QuestionItem QueryQuestionbyID(char *ID);
QuestionItem* QueryQuestionArray();
};
Txml.cpp
int Txml::LoadXml(const char *XmlFilePath)
{
//加载XmlFilePath
//将其读到QuestionItemArray中
}
{
//加载XmlFilePath
//将其读到QuestionItemArray中
}
QuestionItem* Txml:: QueryQuestionArray()//注意声明的返回的是自定义结构的指针
{
//注意返回的是数组名(地址)
//此前犯错,返回第一个元素QuestionIte[0]
return QuestionItemArray;
}
{
//注意返回的是数组名(地址)
//此前犯错,返回第一个元素QuestionIte[0]
return QuestionItemArray;
}
DLL实现如下:
MyDLL.h
extern "C" __declspec(dllexport) void *FillQuestionItem(const char* FileName);
MyDLL.cpp
void *FillQuestionItem(const char* FileName)
{
QuestionItem *QuestionItemP;
Txml *objTxml= new Txml();
objTxml->LoadXml(FileName);
QuestionItemP = (objTxml->QueryQuestionArray());
return QuestionItemP;
}
{
QuestionItem *QuestionItemP;
Txml *objTxml= new Txml();
objTxml->LoadXml(FileName);
QuestionItemP = (objTxml->QueryQuestionArray());
return QuestionItemP;
}
应用层调用的核心部分:
View Code
//加载测试配置文件
void CMyExam2Dlg::LoadTXml(const char *szXmlFileToSave)
{
typedef void* (*pFillQuestionItem)(const char* FileName);
HINSTANCE HDLL; //定义句柄
pFillQuestionItem FillQuestionItem;
HDLL=LoadLibrary(MyDLLPath);
//加载动态链接库FIRSTDll.dll文件,注意指明FIRSTDLL的地址哦
if(HDLL==NULL)
::OutputDebugString("cannot find the dll file");
int my_int = *(int*)GetProcAddress(HDLL, "nMyDLL");
FillQuestionItem=(pFillQuestionItem)GetProcAddress(HDLL,"FillQuestionItem");
QuestionItem *QuestionItemP;
QuestionItem QuestionItemP1[10];
QuestionItemP=(QuestionItem*)FillQuestionItem(szXmlFileToSave );
memcpy(QuestionItemP1, QuestionItemP, sizeof(QuestionItem) * 10);
FreeLibrary(HDLL);//卸载MyDll.dll文件;
}
void CMyExam2Dlg::LoadTXml(const char *szXmlFileToSave)
{
typedef void* (*pFillQuestionItem)(const char* FileName);
HINSTANCE HDLL; //定义句柄
pFillQuestionItem FillQuestionItem;
HDLL=LoadLibrary(MyDLLPath);
//加载动态链接库FIRSTDll.dll文件,注意指明FIRSTDLL的地址哦
if(HDLL==NULL)
::OutputDebugString("cannot find the dll file");
int my_int = *(int*)GetProcAddress(HDLL, "nMyDLL");
FillQuestionItem=(pFillQuestionItem)GetProcAddress(HDLL,"FillQuestionItem");
QuestionItem *QuestionItemP;
QuestionItem QuestionItemP1[10];
QuestionItemP=(QuestionItem*)FillQuestionItem(szXmlFileToSave );
memcpy(QuestionItemP1, QuestionItemP, sizeof(QuestionItem) * 10);
FreeLibrary(HDLL);//卸载MyDll.dll文件;
}
自定义的xml结构
View Code
<?xml version="1.0" encoding="gb2312" ?>
- <resource id="20110512171112">
- <Question id="0">
<Content>1</Content>
<ContentAudio>.\01.mp3</ContentAudio>
<Choice STYLE="TEXT">a</Choice>
<Choice STYLE="TEXT">b</Choice>
<Choice STYLE="TEXT">c</Choice>
<Choice STYLE="TEXT">d</Choice>
<Answer>B</Answer>
</Question>
- <Question id="1">
<Content>2</Content>
<ContentAudio>.\02.mp3</ContentAudio>
<Choice STYLE="TEXT">2</Choice>
<Choice STYLE="TEXT">2</Choice>
<Choice STYLE="TEXT">2</Choice>
<Choice STYLE="TEXT">2</Choice>
<Answer>B</Answer>
</Question>
......
repeat more 8 times
</resource>
- <resource id="20110512171112">
- <Question id="0">
<Content>1</Content>
<ContentAudio>.\01.mp3</ContentAudio>
<Choice STYLE="TEXT">a</Choice>
<Choice STYLE="TEXT">b</Choice>
<Choice STYLE="TEXT">c</Choice>
<Choice STYLE="TEXT">d</Choice>
<Answer>B</Answer>
</Question>
- <Question id="1">
<Content>2</Content>
<ContentAudio>.\02.mp3</ContentAudio>
<Choice STYLE="TEXT">2</Choice>
<Choice STYLE="TEXT">2</Choice>
<Choice STYLE="TEXT">2</Choice>
<Choice STYLE="TEXT">2</Choice>
<Answer>B</Answer>
</Question>
......
repeat more 8 times
</resource>
PS:
感谢师傅指导.