#16
|
|||
|
|||
Quote:
In MASM32 we can invoke function using prototype or call function without prototype. I tend to use the invoke. It is convenient, however it is necessary to get used to. Last edited by dosprog; 04-06-2018 at 07:17. |
The Following User Says Thank You to dosprog For This Useful Post: | ||
Indigo (07-19-2019) |
#17
|
|||
|
|||
its just one more way to call api's, i am just saying use it if you want to else dont. i never said it is the best way to call an api, it is just another way. as i said previously it is subjective you like your own way of programming same way everyone like thiers.
IT IS JUST ONE ANOTHER WAY OF DOING IT. |
#18
|
|||
|
|||
Sure, it's another way of doing it (i never questioned that), and I argued rationally why it doesn't give significant benefits, what it's downsides are, and thus why it isn't worth the trouble.
If you don't want your advice to be discussed and have the pros & cons be weighted against each other, then why even post it? This forum obviously should foster technical discussions, which is exactly what i did. Note that not everyone has the ability to discriminate good from bad solutions, so it's valuable to have these kinds of discussions in order to avoid spreading non-ideal solutions. Also, no need for screaming... |
#19
|
|||
|
|||
) Ok.
I'm written once more usage example of DynCall(). [minor fixed] See attached archive. Also it is here - Pass: exetools Last edited by dosprog; 04-06-2018 at 17:51. |
#20
|
|||
|
|||
Taking what I said previously into account, a much safer variant could look like this:
Code:
template < typename RetType, typename... ArgTypes > RetType DynCall(const char* dll_name, const char* api_name, ArgTypes...args) { typedef RetType(__stdcall* F)(ArgTypes...); HMODULE module = LoadLibrary(dll_name); assert(module != INVALID_MODULE_HANDLE && "Failed to load library"); F fptr = (F)GetProcAddress(module, api_name); assert(fptr && "Unable to resolve API"); return fptr(args...); } Code:
int main() { std::cout << "GetTickCount1: " << DynCal< DWORD >("kernel32.dll", "GetTickCount") << "\n"; DynCall< VOID, DWORD >("kernel32.dll", "Sleep", 1000); std::cout << "GetTickCount2: " << DynCall< DWORD >("kernel32.dll", "GetTickCount") << "\n" } produces this output: Code:
GetTickCount1: 16462234 GetTickCount2: 16463250 Press any key to continue . . . The advantage here is that the compiler does all the type checking for you, so this cannot fail unless you mess up the function prototype itself - but that problem cannot be fixed anyhow unless we're using a correct import library which would defeat the whole purpose of this exercise. Things to improve:
Edit: the template argument do not display correctly due to forum system :/ I fixed it by adding a blank to the template arguments, now it displays correctly. Last edited by mcp; 04-06-2018 at 18:37. |
#21
|
|||
|
|||
sorry for taking criticism in a wrong way, thank you, for your valuable opinions, they are appreciated.
appologies |
The Following User Says Thank You to 0xall0c For This Useful Post: | ||
Indigo (07-19-2019) |
#23
|
|||
|
|||
Another idea worth exploring could be to return a functor from DynCall (instead of invoking the API directly). The functor would basically just capture the function pointer in the c'tor, and provide an overloaded operator(), so the user could call the same API as often as they like (it would also make the code more readable). The functor could then invoke FreeLibrary in the d'tor to free the OS resources.
|
The Following User Says Thank You to mcp For This Useful Post: | ||
Indigo (07-19-2019) |
#25
|
|||
|
|||
i have to deal a lot w/ dyn sym libs/calls for my stuff @samygo and came up w/ some self-including .h file.
its basically for *nix but i ve hacked some windows support though not all features are available there - should compile as C and C++ - type safe - easy to use (imho) - supports logging here some example usage: PHP Code:
PHP Code:
PHP Code:
|
#26
|
|||
|
|||
just for the fun of it, a c++14 version
PHP Code:
PHP Code:
|
#27
|
|||
|
|||
What is C++14 specific about this code?
One drawback is that it's not thread safe. |
The Following User Says Thank You to mcp For This Useful Post: | ||
Indigo (07-19-2019) |
#28
|
|||
|
|||
auto as return type is c++14 specific.
well thread safety wasn't my goal but it should be virtually thread safe. i mean the worst thing that can happen is that you replace a already loaded lib which isn't much of a problem |
The Following User Says Thank You to bugficks For This Useful Post: | ||
Indigo (07-19-2019) |
#29
|
|||
|
|||
Ah right, didn't spot auto return type deduction.
Data races yield undefined behavior, so the worst thing that could happen is that this code produces hard to find bugs/crashes. There is no such thing as "virtually thread safe" |
The Following User Says Thank You to mcp For This Useful Post: | ||
Indigo (07-19-2019) |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Calling any function dynamically without typedef | Succubus | Source Code | 0 | 10-21-2021 16:34 |
[MASM Source] - ZwCreateThread example (winAPI CreateThread emulation) | TomaHawk | Source Code | 4 | 09-08-2019 14:06 |
WinAPI: No WM_COMMAND Message? | aldente | General Discussion | 2 | 07-05-2006 07:17 |