c - Is HINSTANCE valid across threads? -
in single .exe application winmain
entry point has hinstance
parameter should pseudo-handle (because equivalent getmodulehandle(null)
returns pseudo-handle, according msdn). suppose it's pseudo because there both special values (such null
mean entry-point module) , constants used return error (anything less 32).
msdn explicitly describes pointer (nowadays equivalent hmodule
) module base address; know may have different meaning 16 bit applications in 32/64 bits world each process has own address space exact value useless, same each instance , absolutely meaningless outside process.
all these said, first question: can (formally, despite msdn seems contradictory) assume hinstance
pointer (even if don't see use this) or it's better assume it's (pseudo) handle (where value opaque)?
let's assume it's value opaque, second question: value valid per-process or per-thread?
we may think process handle valid per-process few (corner) cases make me think should assume it's valid per-thread. if these corner cases exist (even if seems work as expected per-process) we're relying on implementation detail, undefined behavior subject change different architectures, versions, environments.
let's see case see issue (code trivial describe scenario): dlls has shared code section may have (even if it's pretty uncommon) shared data section (for example share expansive data across processes or implement quick-and-dirty ipc mechanism). may uncommon possible (and pretty easy implement, example in vc++, few #pragma
s data_seg
, comment(linker)
directives). in case we're aware (inside our dll) can't compare hinstance
s (because may have same value) imo can't trust hinstance
had thread (within our dll) comparable hinstance
have in thread b. in short: each time need hinstance
have call getmodulehandle(null)
actual per-thread valid one.
as bonus understand how applies hinstance
parameter when comes winmain
. in theory it's per-thread but, example, createwindow()
resolve correctly (because scope current executing process, of course assuming calling thread has own message loop)?
void createtoolbox(const char* windowname, hinstance hinstance) { // ... // need this? // hinstance hinstance = getmodulehandle(null); createwindow(windowclass, windowname, ws_overlappedwindow, 0, 0, 640, 480, 0, 0, hinstance, null); // ... }
edit seems wrong, did remember thread affinity hmodule applies windows mobile...
if parameter null, getmodulehandle returns pseudo handle current process. [...] pseudo handle special constant interpreted current thread handle. calling thread can use handle specify when thread handle required.
obviously isn't true desktop applications (among other differences).
each module in process has module handle base address of module. hinstance
argument passed winmain
base address of main module of process. such, valid throughout process because process has single virtual address space.
it case hinstance
argument passed winmain
equal getmodulehandle(null)
.
you can treat module handles opaque if wish. is, don't need de-reference pointer , pass along api functions require hmodule
arguments. none of changes whether or not value valid in different threads. value per-process value.
i cannot make sense of bonus question. suspect stems mistake assumption module handles per-thread. once accept module handle has same meaning in threads within process, vast majority of question dissolves.