转载 CreateThread()使用实例 createthread返回值

原文地址:CreateThread()使用实例作者:半路出家一、函数解释

(参见MSDN)

The CreateThread function creates a thread toexecute within the virtual address space of the callingprocess.

To create a thread that runs in the virtual address space ofanother process, use the CreateRemoteThread function.

HANDLE CreateThread(  LPSECURITY_ATTRIBUTES lpThreadAttributes,  SIZE_T dwStackSize,  LPTHREAD_START_ROUTINE lpStartAddress,  LPVOID lpParameter,  DWORD dwCreationFlags,  LPDWORD lpThreadId);

Parameters

lpThreadAttributes
[in] Pointer to a SECURITY_ATTRIBUTES structure thatdetermines whether the returned handle can be inherited by childprocesses. If lpThreadAttributes is NULL, the handle cannotbe inherited.

The lpSecurityDescriptor member of the structurespecifies a security descriptor for the new thread. IflpThreadAttributes is NULL, the thread gets a defaultsecurity descriptor. The ACLs in the default security descriptorfor a thread come from the primary token of the creator.

WindowsXP/2000/NT:The ACLs in thedefault security descriptor for a thread come from the primary orimpersonation token of the creator. This behavior changed withWindowsXP SP2 and WindowsServer2003.
dwStackSize
[in] Initial size of the stack, in bytes. The system roundsthis value to the nearest page. If this parameter is zero, the newthread uses the default size for the executable. For moreinformation, see Thread Stack Size.
lpStartAddress
[in] Pointer to the application-defined function to be executedby the thread and represents the starting address of the thread.For more information on the thread function, see ThreadProc.
lpParameter
[in] Pointer to a variable to be passed to the thread.
dwCreationFlags
[in] Flags that control the creation of the thread. If theCREATE_SUSPENDED flag is specified, the thread is created in asuspended state, and will not run until the ResumeThread function is called. If this value is zero,the thread runs immediately after creation.

If the STACK_SIZE_PARAM_IS_A_RESERVATION flag is specified, thedwStackSize parameter specifies the initial reserve size ofthe stack. Otherwise, dwStackSize specifies the commitsize.

Windows2000/NT and WindowsMe/98/95:TheSTACK_SIZE_PARAM_IS_A_RESERVATION flag is notsupported.
lpThreadId
[out] Pointer to a variable that receives the threadidentifier. If this parameter is NULL, the thread identifier is notreturned.
WindowsMe/98/95:This parameter maynot be NULL.

Return Values

If the function succeeds, the return value is a handle to thenew thread.

If the function fails, the return value is NULL. To get extendederror information, call GetLastError.

Note that CreateThread may succeed even iflpStartAddress points to data, code, or is not accessible.If the start address is invalid when the thread runs, an exceptionoccurs, and the thread terminates. Thread termination due to ainvalid start address is handled as an error exit for the thread'sprocess. This behavior is similar to the asynchronous nature ofCreateProcess, where the process is created even if itrefers to invalid or missing dynamic-link libraries (DLLs).

WindowsMe/98/95:CreateThreadsucceeds only when it is called in the context of a 32-bit program.A 32-bit DLL cannot create an additional thread when that DLL isbeing called by a 16-bit program.

二、使用实例

1.定义的全局变量

DWORD WINAPI ClientThread(LPVOIDlpParam);
struct ClientInfo
{
SOCKET sock;
SOCKADDR_IN clientAddr;////定义地址族
};

2.使用方法

HANDLEhThread;
DWORD dwThread;

struct ClientInfo*pClientInfo=NULL;

pClientInfo=(struct ClientInfo*)malloc(sizeof(struct ClientInfo));

hThread =CreateThread(NULL,0,ClientThread,(LPVOID)pClientInfo,0,&dwThread);
//free(pClientInfo);
if(hThread==NULL)
{
AfxMessageBox("ThreadCreat Failed!n");
return;
}

CloseHandle(hThread);

3.线程函数的实现

DWORD WINAPI ClientThread(LPVOIDlpParam)
{
[转载]CreateThread()使用实例 createthread返回值
struct ClientInfo *pClinetInfo=(struct ClientInfo*)lpParam;

SOCKET sock= pClinetInfo->sock;
SOCKADDR_INaddrClient=pClinetInfo->clientAddr;
free(lpParam);
CTCPServerDlg*dlg=(CTCPServerDlg*)AfxGetApp()->GetMainWnd();

while(1)

{

.....

Sleep(200);

}

return 0;

  

爱华网本文地址 » http://www.aihuau.com/a/25101018/373207.html

更多阅读

会员管理软件储值卡 的使用方法 会员储值卡使用说明

会员管理软件(储值卡)的使用方法——简介 日常生活中,我们的商家经常会需要使用会员管理软件,其中一种类型是储值卡会员管理软件。什么是储值卡,很简单,比如去超市买东西办卡,可以预充值,消费的时候从卡里扣除费用。很多行业需要储值卡,如小

转载 AD620使用的问题 微信转载文章授权问题

原文地址:AD620使用的问题作者:ad620仪表放大器AD620在现今的电路设计中运用越来越广,内部经典的三运放结构有效的减小了共模输入的干扰。但在实际的运用中有一些问题值得注意。1.大家都知道,放大器的输出电压范围取决于其供电电压.但

转载 除草剂使用技术大全--(中国农药第一网) 小麦除草剂大全

原文地址:除草剂使用技术大全--(中国农药第一网)作者:除草剂使用技术大全用技术大全  2,4-滴丁酯中文通用名称2,4一滴丁酯    英文通用名称2,4-Dbutylate    毒性根据中国农药毒性分级标准,2,4一滴丁酯属低毒除草剂。    作用

转载 Tor使用详解 转载 嫁娶择日详解

原文地址:Tor使用详解作者:学无止境一.安装Vidalia下载完成后就可以开始安装了,首先双击下载的安装程序启动安装界面,点击”Next”开始安装(图1),如果你的系统中没有安装Firefox浏览器,软件还会提醒你安装Firefox(火狐)浏览器,如果你不想安

声明:《转载 CreateThread()使用实例 createthread返回值》为网友掏心丿分享!如侵犯到您的合法权益请联系我们删除