比如:
Dim hThread As Long, hThreadID As Long, Tcm As Long
MciCommand = "play C:\WINDOWS\Media\chimes.wav"
Lb = LoadLibrary("winmm.dll") '載入模塊
ProcAdd = GetProcAddress(Lb, "mciExecute") '取得函式入口
hThread = CreateThread(ByVal 0&, ByVal 0&, ProcAdd, ByVal MciCommand, ByVal 0&, hThreadID) '創建執行緒
mciExecute這個函式會回傳一個long的值,要怎樣才可以取得
uj5u.com熱心網友回復:
API函式: GetExitCodeThread( )樓主查一下相差資訊吧。
uj5u.com熱心網友回復:
Function GetExitCodeThread Lib "Kernel32" (ByVal hThread As Long, ByRef ExitCode As Long) As Long如果API呼叫執行成功,ExitCode的回傳值是 STILL_ACTIVE 表示執行緒還沒有結束(還在執行中)。
uj5u.com熱心網友回復:
不是的,我不是要CreateThread這個的回傳值,我是想要CreateThread呼叫的函式的回傳值,也是就CreateThread的第三個引數對應的函式的回傳值。uj5u.com熱心網友回復:
CreateThread( ) 的“第三個引數”,是“輸入引數”,不是用來“回傳資料”的!
注意:VB6的“API瀏覽器”帶的宣告資訊 Win32API.txt ,它的第3個引數形式宣告是錯誤的(某些“修改版”同樣的錯誤)!
VB6中應該加上 ByVal 才對!在Win32API.txt中是沒有指定“傳遞方式”的,那就是默認的ByRef傳遞。
若不更正,如果要“正確傳遞”,那么就要在呼叫CreateThread( ) 陳述句的第3個引數前加ByVal,
否則傳入的不是“函式地址”,而是傳入的“變數地址”或“臨時變數的地址”了。
可以查閱MSDN,仔細“理解”那個引數的作用,可以看到它是“輸入”用的、是“函式入口首址”。
你要得到那個“函式回傳值”,就是需要通過我在樓上說的那個API函式 !
在執行緒創建成功、并且執行緒函式被正常執行完成后,這個“函式”就有一個“回傳值”,
而這個值就需要用 GetExitCodeThread( ) 才能獲取!
uj5u.com熱心網友回復:
CreateThreadThe CreateThread function creates a thread to execute within the address space of the calling process.
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to security attributes
DWORD dwStackSize, // initial thread stack size
LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function
LPVOID lpParameter, // argument for new thread
DWORD dwCreationFlags, // creation flags
LPDWORD lpThreadId // pointer to receive thread ID
);
Parameters
lpThreadAttributes
Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpThreadAttributes is NULL, the handle cannot be inherited.
Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the new thread. If lpThreadAttributes is NULL, the thread gets a default security descriptor.
dwStackSize
Specifies the initial commit size of the stack, in bytes. The system rounds this value to the nearest page. If this value is zero, or is smaller than the default commit size, the default is to use the same size as the calling thread. For more information, see Thread Stack Size.
lpStartAddress
Pointer to the application-defined function of type LPTHREAD_START_ROUTINE to be executed by the thread and represents the starting address of the thread. For more information on the thread function, see ThreadProc.
lpParameter
Specifies a single 32-bit parameter value passed to the thread.
dwCreationFlags
Specifies additional flags that control the creation of the thread. If the CREATE_SUSPENDED flag is specified, the thread is created in a suspended state, and will not run until the ResumeThread function is called. If this value is zero, the thread runs immediately after creation. At this time, no other values are supported.
lpThreadId
Pointer to a 32-bit variable that receives the thread identifier.
Windows NT: If this parameter is NULL, the thread identifier is not returned.
Windows 95 and Windows 98: This parameter may not be NULL.
Return Values
If the function succeeds, the return value is a handle to the new thread.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
Windows 95 and Windows 98: CreateThread succeeds 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 is being called by a 16-bit program.
Remarks
The new thread handle is created with THREAD_ALL_ACCESS to the new thread. If a security descriptor is not provided, the handle can be used in any function that requires a thread object handle. When a security descriptor is provided, an access check is performed on all subsequent uses of the handle before access is granted. If the access check denies access, the requesting process cannot use the handle to gain access to the thread.
The thread execution begins at the function specified by the lpStartAddress parameter. If this function returns, the DWORD return value is used to terminate the thread in an implicit call to the ExitThread function. Use the GetExitCodeThread function to get the thread's return value.
The CreateThread function may succeed even if lpStartAddress points to data, code, or is not accessible. If the start address is invalid when the thread runs, an exception occurs, and the thread terminates. Thread termination due to a invalid start address is handled as an error exit for the thread's process. This behavior is similar to the asynchronous nature of CreateProcess, where the process is created even if it refers to invalid or missing dynamic-link libraries (DLLs).
The thread is created with a thread priority of THREAD_PRIORITY_NORMAL. Use the GetThreadPriority and SetThreadPriority functions to get and set the priority value of a thread.
When a thread terminates, the thread object attains a signaled state, satisfying any threads that were waiting on the object.
The thread object remains in the system until the thread has terminated and all handles to it have been closed through a call to CloseHandle.
The ExitProcess, ExitThread, CreateThread, CreateRemoteThread functions, and a process that is starting (as the result of a call by CreateProcess) are serialized between each other within a process. Only one of these events can happen in an address space at a time. This means that the following restrictions hold:
During process startup and DLL initialization routines, new threads can be created, but they do not begin execution until DLL initialization is done for the process.
Only one thread in a process can be in a DLL initialization or detach routine at a time.
ExitProcess does not return until no threads are in their DLL initialization or detach routines.
A thread that uses functions from the C run-time libraries should use the beginthread and endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called.
Windows CE: The lpThreadAttributes parameter must be set to NULL. The dwStackSize parameter must be zero. Only zero or CREATE_SUSPENDED values are supported for the dwCreationFlags parameter.
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.01 or later.
Header: Declared in winbase.h.
Import Library: Use kernel32.lib.
See Also
Processes and Threads Overview, Process and Thread Functions, CloseHandle, CreateProcess, CreateRemoteThread, ExitProcess, ExitThread, GetExitCodeThread, GetThreadPriority, ResumeThread, SetThreadPriority, SECURITY_ATTRIBUTES, ThreadProc
uj5u.com熱心網友回復:
出差才回來Private Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
這是CreateThread 定義,和你說的一樣
GetExitCodeThread這個不是取執行緒狀態的函式嗎
我想問的是: GetExitCodeThread 創建了一個執行緒,這個執行緒呼叫了 mciExecute 這個API,現在需要得到 mciExecute 這個的回傳值
uj5u.com熱心網友回復:
看清楚:GetExitCodeThread → Get Exit Code Threaed
簡單的“望文生義”,也知道不是“取執行緒狀態”啊…………

自己看看“百度百科”中的介紹啊,那是中文的,你不會說“不認識漢字”吧。
uj5u.com熱心網友回復:
樓主問的是,創建一個執行緒,呼叫執行緒中的函式,希望獲取函式的回傳值。uj5u.com熱心網友回復:
說實話。。。 我覺得這種呼叫方式有點bt。。。單開一個執行緒只為呼叫一個api,是為了異步嗎?
而且常規方法肯定是不可能得到你想要的內容的
uj5u.com熱心網友回復:
《Windows核心編程》《深入決議Windows作業系統-Windows Internals》
uj5u.com熱心網友回復:
已經把你“指引到門口”了,還不能明白,我也只能呵呵了…………
uj5u.com熱心網友回復:
Dim x As Long
x = Me.hwnd
Lb = LoadLibrary("user32.dll") '載入模塊
ProcAdd = GetProcAddress(Lb, "IsWindow") '取得函式入口(判斷是不是視窗)
hThread = CreateThread(ByVal 0&, ByVal 0&, ByVal ProcAdd, ByVal x, ByVal 0&, hThreadID) '創建執行緒
WaitObj = WaitForSingleObject(hThread, INFINITE) '等待執行緒結束
Dim Tcm As Long
GetExitCodeThread hThread, Tcm '取執行緒的反回值
Debug.Print Tcm
FreeLibrary Lb
上面這個代碼,只呼叫有一個引數和一個反回值的API, 如查x是一個有效的視窗句柄,會列印出來1,如果不是有效的視窗句柄會列印出來0,不知道這是不是IsWindow這個函式的回傳值。
uj5u.com熱心網友回復:
我在上面已經說的很直接了。都到這步程度了,竟然還說“不知道這是不是IsWindow這個函式的回傳值”……

樓主,我建議你以后到CSDN,逛這個版塊吧: http://bbs.csdn.net/forums/FreeZone
編程技術的版塊不適合你。
uj5u.com熱心網友回復:
既然你這么勵害,那你給我說說我想傳多個叁數要怎么作呢uj5u.com熱心網友回復:
直接傳給“執行緒”的引數,只能是1個,這是“約定”、是別人定好的“游戲規則”,如果你“要走這條路”,那就是無法改變的事實!
但是,它不會去管你這“1個引數”具體的是什么含義。
也許這剛好是我需要傳遞的一個資料值;
也有可能什么都不是,只是因為“要填補空缺”而已,這個“引數”我根本就不需要……
以“Win32環境”來說(就比如VB6吧,其它開發環境也可以參考),那個“執行緒引數”是個“32位數”,
但需要傳遞“2個、或更多個引數”,是不能直接通過CreateThread( )傳遞的。
不過,其實“多個引數”,可以事先處理、放到一個“1維陣列”中,而這個“執行緒引數”則是傳遞它的“資料首址”;
在“執行緒函式”中,按“約定順序”和資料首址值,依次取出資料就行了。
當然這種情況需要“自己包裝”一下,以便于正確接收及還原傳入的引數,
肯定不能象你上面12樓那樣,直接用到系統的(或其它第三方DLL的)API函式上。
用這種方式“包裝”,別說三個引數,就是300個也沒問題。并且可以是不同型別的引數混合的……
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/49975.html
標籤:API
上一篇:錯誤提示Else沒有If
下一篇:rsa加密
