dll中创建win32窗口出现的崩溃有关问题

求助:dll中创建win32窗口出现的崩溃问题
我在dll的入口函数中启动一个创建win32窗口的线程 ,在线程函数中注册窗口、创建窗口和消息循环,如下代码:

#include "stdafx.h"
#include "stdafx.h"
#include "WinForm.h"
#include <process.h>

HINSTANCE g_hInstance;
int flag = 0;

void c(void* )
{
flag = 1;
MyCreateWindow(g_hInstance,NULL, 0, 1);
return;
}

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
 )
{
g_hInstance = (HINSTANCE)hModule;
if (0 == flag)
{
_beginthread(c, NULL, NULL);
flag = 1;
}

switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

然后线程函数里的代码如下:

#include "stdafx.h"
#include "Winform.h"
#include <Windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>

static TCHAR szWindowClass[] = TEXT("win32app");

static TCHAR szTitle[] = TEXT("Win32 Guided Tour Application");

HINSTANCE hInst;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY MyCreateWindow(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR    lpCmdLine,
int       nCmdShow)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style          = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc    = WndProc;
wcex.cbClsExtra     = 0;
wcex.cbWndExtra     = 0;//DLGWINDOWEXTRA ;
wcex.hInstance      = hInstance;
wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName   = NULL;
wcex.lpszClassName  = szWindowClass;
wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

if (!RegisterClassEx(&wcex))
{
DWORD d = GetLastError();

return 1;
}

hInst = hInstance; 

HWND hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
DWORD d = GetLastError();

return 1;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return (int) msg.wParam;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
TCHAR greeting[] = TEXT("Hello, World!");

switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);

// Here your application is laid out.
// For this introduction, we just print out "Hello, World!"
// in the top left corner.
TextOut(hdc,
5, 5,
greeting, _tcslen(greeting));
// End application-specific layout section.

EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}

return 0;
}

在这个dll中某个函数被调用的时候,进入入口函数便启动该线程,我发现线程函数中注册窗口成功了,但是CreateWindow这个函数执行时就崩溃,如下
dll中创建win32窗口出现的崩溃有关问题
请教大神,这个什么原因啊,该怎么解决?小弟在此不胜感激!!dll中创建win32窗口出现的崩溃有关问题
------解决方案--------------------
这么好的问题,我居然不会,顶一下
------解决方案--------------------
dllmain函数,msdn有警告说明
Calling Win32 functions other than TLS, synchronization, and file functions may result in problems that are difficult to diagnose. For example, calling User, Shell, COM, RPC, and Windows Sockets functions (or any functions that call these functions) can cause access violation errors, because their DLLs call LoadLibrary to load other system components.