有的api函数需要用LoadLibrary,GetProcAddress查找这个函数的过程实现
#include
#include
#include
#include
typedef BOOL (WINAPI* _AttachConsole)(DWORD dwProcessId);
BOOL APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
hInstance = LoadLibrary(_T("kernel32.dll"));
if (hInstance == NULL)
{
return FALSE;
}
_AttachConsole AttachConsole = NULL;
AttachConsole = (_AttachConsole)GetProcAddress(hInstance , "AttachConsole");
if (AttachConsole == NULL)
{
return FALSE;
}
if (0 == AttachConsole((DWORD)-1) )
{
MessageBox(0, _T("AttachConsole fail"), _T("msg"), 0);
}
else
{
SetConsoleTitle("Hello!");
freopen("conin$", "r+t", stdin);
freopen("conout$", "w+t", stdout);
freopen("conout$", "w+t", stderr);
puts("\nHello, world!");
system("pause>nul");
FreeConsole();
}
return 0;
}
把这段代码编译生成一个简单的GUI程序(exe文件),然后打开一个控制台窗口,输入这个exe文件的路径,运行后,这个程序就会在控制台窗口中输出hello world