DLLMain能够执行,但里面的内容不输出的问题,求大神指教!

[复制链接]
查看11 | 回复7 | 2021-1-27 06:52:09 | 显示全部楼层 |阅读模式
小弟初学DLL编写。环境是Codeblocks10.05,编译器是MinGW。
首先新建一个dll工程,命名为mydll。
mydll.h文件内容:
#ifndef__MYDLL_H__
#define__MYDLL_H__
#include
/*Tousethisexportedfunctionofdll,includethisheader
*inyourproject.
*/
#ifdefBUILD_MY_DLL
#defineDLL_EXPORT__declspec(dllexport)
#else
#defineDLL_EXPORT__declspec(dllimport)
#endif

#ifdef__cplusplus
extern"C"
{
#endif
intDLL_EXPORTsay(constchar*s);
#ifdef__cplusplus
}
#endif
#endif//__MAIN_H__

mydll.cpp文件内容:
#include
#include
#include"mydll.h"
intDLL_EXPORTsay(constchar*s)
{
printf("%s\n",s);
return-1;
}
BOOLWINAPIDllMain(HINSTANCEhinstDLL,DWORDfdwReason,LPVOIDlpvReserved)
{
//MessageBox(NULL,"MessageBoxText(内容)","Title(标题)",MB_OK);
//freopen("OUTPUT.txt","w",stdout);
printf("被调用\n");
switch(fdwReason)
{
caseDLL_PROCESS_ATTACH:
//returnFALSEtofailDLLload
printf("attachtoprocess\n");
break;
caseDLL_PROCESS_DETACH:
printf("detachfromprocess\n");
break;
caseDLL_THREAD_ATTACH:
printf("attachtothread\n");
break;
caseDLL_THREAD_DETACH:
printf("detachtothread\n");
break;
}
returnTRUE;//succesful
}

加入宏BUILD_MY_DLL,编译链接成功,生成mydll.dll动态链接库文件和libmydll.dll.a文件(lib文件)。
使用mydll.dll文件:
新建工程,将mydll.dll文件拷贝到工程目录,main.cpp文件:
/**显式加载DLL中的函数*/
#include
#include
usingnamespacestd;
intmain()
{
typedefint(*DLL_FUNC_PTR)(constchar*s);
DLL_FUNC_PTRfunc=NULL;
HINSTANCEhinst=::LoadLibrary("mydll.dll");
if(hinst==NULL)
{
cout
上述执行成功,输出内容为:
helloworld!
-1
DllMain函数的内容却没有输出,写到文件里也不行,请问大牛们这是什么原因。
分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:52:09 | 显示全部楼层
高深,没看懂
回复

使用道具 举报

千问 | 2021-1-27 06:52:09 | 显示全部楼层
MethodsforDebuggingDLLs
IfyouhavethesourceforboththeDLLandthecallingprogram,opentheprojectforthecallingexecutablefileanddebugtheDLLfromthere.IfyouloadaDLLdynamically,youmustspecifyitintheAdditionalDLLscategoryoftheDebugtabintheProjectSettingsdialogbox.
IfyouhavethesourcefortheDLLonly,opentheprojectthatbuildstheDLL.UsetheDebugtabintheProjectSettingsdialogboxtospecifytheexecutablefilethatcallstheDLL.
YoucanalsodebugaDLLwithoutaproject.Forexample,maybeyoujustpickedupaDLLandsourcecodebutyoudon'thaveanassociatedprojectorworkspace.YoucanusetheOpencommandontheFilemenutoselectthe.DLLfileyouwanttodebug.Thedebuginformationshouldbeineitherthe.DLLortherelated.PDBfile.AfterVisualC++opensthefile,ontheBuildmenuclickStartDebugandGotobegindebugging.
TodebugaDLLusingtheprojectfortheexecutablefile
1.FromtheProjectmenu,clickSettings.
TheProjectSettingsdialogboxappears.
2.ChoosetheDebugtab.
3.IntheCategorydrop-downlistbox,selectGeneral.
4.IntheProgramArgumentstextbox,typeanycommand-lineargumentsrequiredbytheexecutablefile.
5.IntheCategorydrop-downlistbox,selectAdditionalDLLs.
6.IntheLocalNamecolumn,typethenamesofDLLstodebug.
Ifyouaredebuggingremotely,theRemoteNamecolumnappears.Inthiscolumn,typethecompletepathfortheremotemoduletomaptothelocalmodulename.
7.InthePreloadcolumn,selectthecheckboxifyouwanttoloadthemodulebeforedebuggingbegins.
8.ClickOKtostoretheinformationinyourproject.
9.FromtheBuildmenu,clickStartDebugandGotostartthedebugger.
YoucansetbreakpointsintheDLLorthecallingprogram.YoucanopenasourcefilefortheDLLandsetbreakpointsinthatfile,eventhoughitisnotapartoftheexecutablefile'sproject.
TodebugaDLLusingtheprojectfortheDLL
1.FromtheProjectmenu,clickSettings.
TheProjectSettingsdialogboxappears.
2.ChoosetheDebugtab.
3.IntheCategorydrop-downlistbox,selectGeneral.
4.IntheExecutableForDebugSessiontextbox,typethenameoftheexecutablefilethatcallstheDLL.
5.IntheCategorylistbox,selectAdditionalDLLs.
6.IntheLocalModuleNamecolumn,typethenameoftheDLLsyouwanttodebug.
7.ClickOKtostoretheinformationinyourproject.
8.SetbreakpointsasrequiredinyourDLLsourcefilesoronfunctionsymbolsintheDLL.
9.FromtheBuildmenu,clickStartDebugandGotostartthedebugger.
TodebugaDLLcreatedwithanexternalproject
1.FromtheProjectmenu,clickSettings.
TheProjectSettingsdialogboxappears.
2.ChoosetheDebugtab.
3.IntheCategorydrop-downlistbox,selectGeneral.
4.IntheExecutableForDebugSessiontextbox,typethenameoftheDLLthatyourexternalmakefilebuilds.
5.ClickOKtostoretheinformationinyourproject.
6.BuildadebugversionoftheDLLwithsymbolicdebugginginformation,ifyoudon'talreadyhaveone.
7.FollowoneofthetwoproceduresimmediatelyprecedingthisonetodebugtheDLL.

回复

使用道具 举报

千问 | 2021-1-27 06:52:09 | 显示全部楼层
无量天尊,这不是秃子头上的虱子,明摆的嘛,改成:
在mydll.h
#ifdef__cplusplus
extern"C"
{
#endif
intDLL_EXPORTsay(constchar*s);
BOOLWINAPIDllMain(HINSTANCEhinstDLL,DWORDfdwReason,LPVOIDlpvReserved);
#ifdef__cplusplus
}
#endi
回复

使用道具 举报

千问 | 2021-1-27 06:52:09 | 显示全部楼层
loadlibrary或free的时候都会调用dllmain按理说楼主的代码应该会执行的,估计是人品问题了
回复

使用道具 举报

千问 | 2021-1-27 06:52:09 | 显示全部楼层
不放到extern"C"里的话,g++会对dllmain进行符号修饰,所以就无法在loadlibrary时被调用了
回复

使用道具 举报

千问 | 2021-1-27 06:52:09 | 显示全部楼层
说白了,这与编译器也有一定的问题,vc的编译器就不需要!!!
回复

使用道具 举报

千问 | 2021-1-27 06:52:09 | 显示全部楼层
引用5楼wangdahu888的回复:不放到extern"C"里的话,g++会对dllmain进行符号修饰,所以就无法在loadlibrary时被调用了
看来我是忽略了vc的优化了,没注意楼主不是vc
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行