svn path=/trunk/; revision=638
This commit is contained in:
Eric Kohl 1999-09-05 10:22:05 +00:00
parent fa74a12186
commit b0e64d6cf7
2 changed files with 52 additions and 26 deletions

View file

@ -21,19 +21,19 @@ thread_main2(LPVOID param)
int main (void) int main (void)
{ {
DWORD i; DWORD i=0;
DWORD id; DWORD id;
printf("Creating %d threads\n",NR_THREADS); printf("Creating %d threads...\n",NR_THREADS);
for (i=0;i<NR_THREADS;i++) // for (i=0;i<NR_THREADS;i++)
{ // {
CreateThread(NULL, CreateThread(NULL,
0, 0,
thread_main1, thread_main1,
(LPVOID)i, (LPVOID)i,
0, 0,
&id); &id);
#if 0
CreateThread(NULL, CreateThread(NULL,
0, 0,
thread_main2, thread_main2,
@ -41,8 +41,11 @@ int main (void)
0, 0,
&id); &id);
} }
#endif
printf("Threads created...\n");
Sleep (5000); // Sleep (5000);
SuspendThread (GetCurrentThread());
return 0; return 0;
} }

View file

@ -17,6 +17,10 @@
#include <string.h> #include <string.h>
#include <internal/i386/segment.h> #include <internal/i386/segment.h>
//#define NDEBUG
#include <kernel32/kernel32.h>
/* FUNCTIONS *****************************************************************/ /* FUNCTIONS *****************************************************************/
HANDLE STDCALL CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, HANDLE STDCALL CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes,
@ -43,7 +47,6 @@ HANDLE STDCALL CreateRemoteThread(HANDLE hProcess,
DWORD dwCreationFlags, DWORD dwCreationFlags,
LPDWORD lpThreadId) LPDWORD lpThreadId)
{ {
NTSTATUS errCode;
HANDLE ThreadHandle; HANDLE ThreadHandle;
OBJECT_ATTRIBUTES ObjectAttributes; OBJECT_ATTRIBUTES ObjectAttributes;
CLIENT_ID ClientId; CLIENT_ID ClientId;
@ -51,6 +54,9 @@ HANDLE STDCALL CreateRemoteThread(HANDLE hProcess,
INITIAL_TEB InitialTeb; INITIAL_TEB InitialTeb;
BOOLEAN CreateSuspended = FALSE; BOOLEAN CreateSuspended = FALSE;
PVOID BaseAddress; PVOID BaseAddress;
DWORD StackSize;
ULONG BytesWritten;
NTSTATUS Status;
ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES); ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
ObjectAttributes.RootDirectory = NULL; ObjectAttributes.RootDirectory = NULL;
@ -70,14 +76,22 @@ HANDLE STDCALL CreateRemoteThread(HANDLE hProcess,
else else
CreateSuspended = FALSE; CreateSuspended = FALSE;
StackSize = (dwStackSize == 0) ? 4096 : dwStackSize;
BaseAddress = 0; BaseAddress = 0;
ZwAllocateVirtualMemory(hProcess, Status = NtAllocateVirtualMemory(hProcess,
&BaseAddress, &BaseAddress,
0, 0,
(PULONG)&dwStackSize, (PULONG)&StackSize,
MEM_COMMIT, MEM_COMMIT,
PAGE_READWRITE); PAGE_READWRITE);
if (!NT_SUCCESS(Status))
{
DPRINT("Could not allocate stack space!\n");
return NULL;
}
DPRINT("Stack base address: %p\n", BaseAddress);
memset(&ThreadContext,0,sizeof(CONTEXT)); memset(&ThreadContext,0,sizeof(CONTEXT));
ThreadContext.Eip = (LONG)lpStartAddress; ThreadContext.Eip = (LONG)lpStartAddress;
@ -87,11 +101,13 @@ HANDLE STDCALL CreateRemoteThread(HANDLE hProcess,
ThreadContext.SegDs = USER_DS; ThreadContext.SegDs = USER_DS;
ThreadContext.SegCs = USER_CS; ThreadContext.SegCs = USER_CS;
ThreadContext.SegSs = USER_DS; ThreadContext.SegSs = USER_DS;
ThreadContext.Esp = (ULONG)(BaseAddress + dwStackSize); ThreadContext.Esp = (ULONG)(BaseAddress + StackSize - 8);
ThreadContext.EFlags = (1<<1) + (1<<9); ThreadContext.EFlags = (1<<1) + (1<<9);
/* write lpParameter to highest stack address */
*((PBYTE)(BaseAddress + StackSize - 4)) = lpParameter;
errCode = NtCreateThread(&ThreadHandle, Status = NtCreateThread(&ThreadHandle,
THREAD_ALL_ACCESS, THREAD_ALL_ACCESS,
&ObjectAttributes, &ObjectAttributes,
hProcess, hProcess,
@ -99,6 +115,13 @@ HANDLE STDCALL CreateRemoteThread(HANDLE hProcess,
&ThreadContext, &ThreadContext,
&InitialTeb, &InitialTeb,
CreateSuspended); CreateSuspended);
if (!NT_SUCCESS(Status))
{
DPRINT("NtCreateThread() failed!\n");
return NULL;
}
if ( lpThreadId != NULL ) if ( lpThreadId != NULL )
memcpy(lpThreadId, &ClientId.UniqueThread,sizeof(ULONG)); memcpy(lpThreadId, &ClientId.UniqueThread,sizeof(ULONG));