您的位置:首页 > 编程语言 > C语言/C++

C++在dll中获取自身路径(非exe调用路径)

2016-08-24 11:07 483 查看
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <windows.h>
using namespace std;

HMODULE GetSelfModuleHandle()
{
MEMORY_BASIC_INFORMATION mbi;
return ((::VirtualQuery(GetSelfModuleHandle, &mbi, sizeof(mbi)) != 0) ? (HMODULE)mbi.AllocationBase : NULL);
}

extern "C" __declspec(dllexport) int GetDllPath(){
ifstream file;
char curDir[100] = {0};
GetModuleFileName(GetSelfModuleHandle(),curDir,100);
cout<<"Dll Path:"<<curDir<<endl;
return 0;
}

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD  ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
cout<<"DllMain called"<<endl;
return TRUE;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: