-
-
Notifications
You must be signed in to change notification settings - Fork 589
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix procedure casting; SUBSYSTEM to CONSOLE in linker
- Loading branch information
1 parent
0d69dfc
commit e8b4228
Showing
4 changed files
with
42 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,55 @@ | ||
#import "win32.odin" | ||
#import "fmt.odin" | ||
|
||
Dll :: struct { | ||
Handle :: type rawptr | ||
name: string | ||
handle: Handle | ||
} | ||
|
||
main :: proc() { | ||
get_proc :: proc(lib: win32.HMODULE, name: string) -> proc() { | ||
buf: [4096]byte | ||
copy(buf[:], name as []byte) | ||
load_library :: proc(name: string) -> (Dll, bool) { | ||
buf: [4096]byte | ||
copy(buf[:], name as []byte) | ||
|
||
proc_handle := win32.GetProcAddress(lib, ^buf[0]) | ||
return proc_handle as proc() | ||
lib := win32.LoadLibraryA(^buf[0]) | ||
if lib == nil { | ||
return nil, false | ||
} | ||
return Dll{name, lib as Dll.Handle}, true | ||
} | ||
|
||
lib := win32.LoadLibraryA(("example.dll\x00" as string).data) | ||
if lib == nil { | ||
free_library :: proc(dll: Dll) { | ||
win32.FreeLibrary(dll.handle as win32.HMODULE) | ||
} | ||
|
||
get_proc_address :: proc(dll: Dll, name: string) -> (rawptr, bool) { | ||
buf: [4096]byte | ||
copy(buf[:], name as []byte) | ||
|
||
addr := win32.GetProcAddress(dll.handle as win32.HMODULE, ^buf[0]) as rawptr | ||
if addr == nil { | ||
return nil, false | ||
} | ||
return addr, true | ||
} | ||
|
||
|
||
main :: proc() { | ||
lib, lib_ok := load_library("example.dll") | ||
if !lib_ok { | ||
fmt.println("Could not load library") | ||
return | ||
} | ||
defer win32.FreeLibrary(lib) | ||
defer free_library(lib) | ||
|
||
|
||
proc_handle := get_proc(lib, "some_thing") | ||
if proc_handle == nil { | ||
proc_addr, addr_ok := get_proc_address(lib, "some_thing") | ||
if !addr_ok { | ||
fmt.println("Could not load 'some_thing'") | ||
return | ||
} | ||
|
||
some_thing := (proc_handle as proc()) | ||
some_thing := (proc_addr as proc()) | ||
fmt.println(some_thing) | ||
some_thing() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters