Lauer
Lauer

Reputation: 517

Extracting specific Win32 functions

I want to use the CreateThread function from windows.h, but without all the #defines and crap that is included in the header file. Is there some way that i could import(I think thats what its called) just that function from the .dll or .lib? tested and failed:

#pragma comment(lib,"Kernel32.lib")
__declspec(dllimport)
unsigned long
__stdcall
WaitForSingleObject(
    void* hHandle,
    unsigned long dwMilliseconds
    );


int main()
{
    WaitForSingleObject(0,0);
}

Upvotes: 0

Views: 190

Answers (3)

James McNellis
James McNellis

Reputation: 354969

There are at least two options:

  1. You don't need to include the header. The definition of CreateThread is not going to change. You can just copy its declaration (and the declarations on which it depends) from the Windows headers into your own source file.

  2. You can write wrappers around the Windows API functions that you use, then "pimpl" usage of the Windows API so that you don't have to include Windows.h and other headers all over the place.

In my opinion, the latter option is probably preferable as it is less error-prone, simpler, and provides isolation of nonstandard functions, making it easier to port the code.

Upvotes: 2

whiterook6
whiterook6

Reputation: 3534

I think DLLs dont have preprocessor directives because they're already compiled, so including the DLL shouldn't pollute your working set. If you want to include the source file (the *.h file), then you will need to compile those functions as well, which may very well depend on those directives and other crap.

Upvotes: 0

James
James

Reputation: 9278

Yes, you can load the DLL dynamically, get the address of the function and then call it. I'm not sure what your problem is with Windows.h though.

LoadLibrary GetProcAddress

Upvotes: 3

Related Questions