PDA

View Full Version : How can I export this to Pascal?



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;

}

Dgby714
01-13-2012, 12:12 AM
You can pass it to Pascal, tho there is no need as Pascal has TThead. (Simba doesn't)

You wont be able to use this in Simba as the functions are not native their interpreted.

Brandon
01-13-2012, 12:14 AM
You can pass it to Pascal, tho there is no need as Pascal has TThead. (Simba doesn't)

You wont be able to use this in Simba as the functions are not native their interpreted.

Awww thats a bummer. So how does smart run it's threads :S Can't load it like smart? So if I were to write this in Pascal fully using TThreads, would Simba be able to pass functions as Pointers to it so that the thread runs it in the background.

Dgby714
01-13-2012, 12:16 AM
Awww thats a bummer. So how does smart run it's threads :S Can't load it like smart?

Yes you can run threads in a library! Tho they can't call any functions in your script.

Brandon
01-13-2012, 12:20 AM
Yes you can run threads in a library! Tho they can't call any functions in your script.

Ahhhh I see what u mean.. man that sucks big time! Thanks anyway :c