WSADATA wsa;
WSAStartup(MAKEWORD(2,2),&wsa);
hostent *pht;
pht=gethostbyname(“www.lingdux.com”);
LPSTR pstr=pht->h_addr_list[0];
struct in_addr inaddr;
memmove(&inaddr,pstr,4);
MessageBoxA(0,inet_ntoa(inaddr),0,0);
CFtpConnection *pconnection;
CInternetSession *session=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);
try
{
pconnection=session->GetFtpConnection(FtpAddress,UserName,Password);
}
catch(CInternetException *pEx)
{
WCHAR errror[1024]={0};
if(pEx->GetErrorMessage(ERROR,1024))
MessageBox(ERROR);
else
MessageBox(L”出现异常”);
}
if(pconnection->PutFile(L”C:\\ip.txt”,path))
MessageBox(L”上传成功!”);
else
MessageBox(L”上传失败!”);
pconnection->Close();
今天抛弃MFC写了一个窗口,用while(GetMessage())进入消息循环
在填写GetMessage()的参数的时候,按照VS2008显示的参数进行填写。GetMessage(&msg,hwnd,0,0)
写好回调后编译,运行,结果点退出之后窗体消失,程序并不退出,打开任务管理器发现CPU飙满。
baidu到一篇和我遇到问题一样,没有解决。
OD载入调试,在GetMessage下断,发现返回-1,加了一句GetLastError()返回0×578,窗体指针无效,豁然开朗。
原来是GetMessage()的第二个参数应该添NULL表示接受所有窗口的消息,我添的是hwnd,DestroyWindow后句柄就无效了,因此GetMessage()返回-1并且进入死循环了。改成NULL就可以正常接收到PostQuitMessage()的消息了!
#include “windows.h”
#include “tchar.h”
WCHAR a[]=L”123″;
int WINAPI MyFonc(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
WCHAR b[]=L”456″;
HMODULE hmodule=LoadLibrary(L”user32.dll”);
FARPROC addr=GetProcAddress(hmodule,”MessageBoxW”);
__asm
{
push 0
mov eax,offset a//全局用offset
push eax
lea eax,b//局部用lea
push eax
push 0
call addr
}
return 0;
}
昨天写win32程序时候用到资源释放,网上一查基本都是用MFC的类和函数实现的,下面的是不用MFC的类和函数实现资源释放的代码:
HINSTANCE h=GetModuleHandle(NULL);
HRSRC hr = FindResource(h, MAKEINTRESOURCE(IDR_SYS1), “SYS”);
HGLOBAL hg = LoadResource(h, hr);
LPVOID pv = (PSZ)LockResource(hg);
HANDLE cr;
cr=CreateFile(“C:\\1.sys”,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,0);
int length = SizeofResource(h,hr);
DWORD dw;
WriteFile(cr,hg,length,&dw,NULL);
CloseHandle(cr);
HANDLE huchi=CreateMutex(NULL,false,”server”);
if(GetLastError()==ERROR_ALREADY_EXISTS)
{
huchi=NULL;
exit(0);;
}
#ifdef NDEBUG
#pragma optimize(“gsy”,on)
//RELEASE方式编译
#pragma comment(linker,”/RELEASE”)
#ifdef _MERGE_RDATA_
//合并区段
#pragma comment(linker,”/merge:.rdata=.text”)
#endif
#pragma comment(linker,”/merge:.data=.text”)
#pragma comment(linker,”/merge:.reloc=.text”)
#if _MSC_VER>=1000
#endif
#endif