Brandon
01-12-2012, 11:55 PM
How can I load this In Simba? It's a DLL for multithreading.. The idea is that it takes in a function pointer and runs that in a thread.. Dunno if it will work in Simba but I thought it was worth a shot to put my time into it. Haven't actually seen anyone attempt it I guess :S
DLL Header:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
/* To use this exported function of dll, include this header
* in your project.
*/
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
void DLL_EXPORT Threading(HANDLE hThread, DWORD ThreadID, void* FunctionToPass);
DWORD DLL_EXPORT ThreadProc(LPVOID lpParameter);
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__
DLL Code:
#include "main.h"
DWORD DLL_EXPORT ThreadProc(LPVOID lpParameter)
{
//Run the function!
void (* function)() = (void (*)())lpParameter;
function();
return 0;
}
void DLL_EXPORT Threading(HANDLE hThread, DWORD ThreadID, void* FunctionToPass)
{
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc, (void*)FunctionToPass, 0, &ThreadID);
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
Usage of the DLL:
#include <windows.h>
#include <iostream>
//DLL Function: void DLL_EXPORT Threading(HANDLE hThread, DWORD ThreadID, void* FunctionToPass);
typedef void (*CThread)(HANDLE hTThread, DWORD TThreadID, void* FuncToPass);
void FunctionToPassX()
{
while(true)
std::cout<<"\nRunning Stuff in another thread :D";
}
int main()
{
HINSTANCE h = LoadLibrary(TEXT("Threading.dll"));
if (h)
{
std::cout<<"DLL Loaded.\n";
CThread TThread;
TThread = (CThread) GetProcAddress(h, "Threading");
if (NULL != TThread)
{
std::cout<<"Function Found.";
Sleep(1000);
HANDLE HThread;
DWORD TThreadID = 1;
TThread(HThread, TThreadID, (void*) FunctionToPassX);
CloseHandle(HThread);
}
FreeLibrary(h);
}
std::cin.get();
return 0;
}
DLL Header:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
/* To use this exported function of dll, include this header
* in your project.
*/
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
void DLL_EXPORT Threading(HANDLE hThread, DWORD ThreadID, void* FunctionToPass);
DWORD DLL_EXPORT ThreadProc(LPVOID lpParameter);
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__
DLL Code:
#include "main.h"
DWORD DLL_EXPORT ThreadProc(LPVOID lpParameter)
{
//Run the function!
void (* function)() = (void (*)())lpParameter;
function();
return 0;
}
void DLL_EXPORT Threading(HANDLE hThread, DWORD ThreadID, void* FunctionToPass)
{
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc, (void*)FunctionToPass, 0, &ThreadID);
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
Usage of the DLL:
#include <windows.h>
#include <iostream>
//DLL Function: void DLL_EXPORT Threading(HANDLE hThread, DWORD ThreadID, void* FunctionToPass);
typedef void (*CThread)(HANDLE hTThread, DWORD TThreadID, void* FuncToPass);
void FunctionToPassX()
{
while(true)
std::cout<<"\nRunning Stuff in another thread :D";
}
int main()
{
HINSTANCE h = LoadLibrary(TEXT("Threading.dll"));
if (h)
{
std::cout<<"DLL Loaded.\n";
CThread TThread;
TThread = (CThread) GetProcAddress(h, "Threading");
if (NULL != TThread)
{
std::cout<<"Function Found.";
Sleep(1000);
HANDLE HThread;
DWORD TThreadID = 1;
TThread(HThread, TThreadID, (void*) FunctionToPassX);
CloseHandle(HThread);
}
FreeLibrary(h);
}
std::cin.get();
return 0;
}