在dll中的代码:
DWORD _stdcall WriteComm(int i_hCom,char* buf, DWORD dwLength)
{
g_buf = buf;
g_hCom = i_hCom;


//fState=WriteFile(m_hCom,buf,length,&length,&m_osWrite);
if (dwLength>4)
{

TIMECAPS tc;
//获得定时器分辨率
if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) != TIMERR_NOERROR) 
{
return 0;
}
UINT nResolution = min(max(tc.wPeriodMin, 1), tc.wPeriodMax); UINT nInterval = 1;
if (nInterval < nResolution)
{
nInterval = nResolution;
}
//设置定时最小分辨率
timeBeginPeriod(nResolution);
//设置定时器
GetLocalTime(&ss);
m_nTimerID = timeSetEvent(5, nResolution,CallBackFuncByFile,NULL, TIME_PERIODIC);
}
return 0;
}
在调用它的主程序中这样使用
        HINSTANCE hInstance;
HANDLE m_hCom;
hInstance = LoadLibrary(_T("port_dll.dll"));
if (!hInstance)
{
AfxMessageBox(_T("Not Find this Dll"));
}
typedef HANDLE (_stdcall *openPort)(int);
openPort OpenConnection = (openPort) GetProcAddress(hInstance,"OpenConnection"); 
typedef void (_stdcall * CLOSECONNECTION)(HANDLE);
CLOSECONNECTION CloseConnection = (CLOSECONNECTION) GetProcAddress(hInstance,"CloseConnection"); 
typedef DWORD (_stdcall * WRITECOMM)(int,char*, DWORD); WRITECOMM WriteComm = (WRITECOMM) GetProcAddress(hInstance,"WriteComm");  char* buf;
char a[10] = {1,2,3,4,5,1,2,3,4,5};
buf = a;WriteComm(5,buf,10);
FreeLibrary(hInstance);
它并不是在运行完回调函数关闭计时器后再调用FreeLibrary(hInstance);而是定时器运行到一半(可能只调用一次回调函数并没有关闭计时器)就开始运行到FreeLibrary(hInstance)然后再回去跑定时器导致程序错误,不知道各位大虾有什么建议吗?急等!!!!!