Hi, I'm a nim noob, I decided to try it because I'm pretty confident using python but I suc* in C/C++ ^_^
I am trying to dump a process in memory using
BOOL MiniDumpWriteDump(
[in] HANDLE hProcess,
[in] DWORD ProcessId,
[in] HANDLE hFile,
[in] MINIDUMP_TYPE DumpType,
[in] PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
[in] PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
[in] PMINIDUMP_CALLBACK_INFORMATION CallbackParam
);
CallbackParam it's a pointer to my callback function where I can store the dump in memory, the problem is that I don't know how to pass this function pointer as argument.
For example if I have
proc callback() =
# do stuff, store in memory
I can't use callback.unsafeAddr as argument
I thought I can use memfiles and instead of using the CallbackParam, using the MemFile.fHandle as hFile param but here I get another problem:
mm: MemFile = memfiles.open("test.mmap", fmWrite, -1)
when I run the program I get:
oserr.nim(95) raiseOSError Error: unhandled exception: Unable to find the specified file.
thank you for your time, I apologize for any nonsense XD
I think I found the solution
type
CallbackHelper = object
dumpBuffer: LPVOID
bytesRead: DWORD
var Helper: CallbackHelper
type
pMiniDumpCallback = proc(CallbackParam: PVOID, CallbackInputParam: PMINIDUMP_CALLBACK_INPUT, CallbackOutputParam: PMINIDUMP_CALLBACK_OUTPUT): BOOL {.stdcall.}
proc MiniDumpCallback(CallbackParam: PVOID, CallbackInputParam: PMINIDUMP_CALLBACK_INPUT, CallbackOutputParam: PMINIDUMP_CALLBACK_OUTPUT): BOOL =
#do stuff
and then
var callback: MINIDUMP_CALLBACK_INFORMATION
callback.CallbackRoutine = cast[pMiniDumpCallback](MiniDumpCallback)
callback.CallbackParam = Helper.addr
MiniDumpWriteDump(bla, bla, bla, bla, bla, bla, callback.addr)
Looks like it's workin, but now I have another issue, see you soon xD
In Nim, you can get a pointer to proc with the procedure name alone and don't need to use addr or unsafeAddr.
When you pass a callback function to C function, function signature and calling convention must match. Otherwise, it is undefined behavior.
cast is unsafe feature in Nim. cast is sometimes required when calling C functions. But in your case, I think you can avoid using cast if you set calling convention correclty to your callback proc.
About calling Conventions in ms windows
I often see people trying to use C libraries or windows API from Nim on Nim forum/discord. And some of them don't seem to know much about C lang, pointers or low level things and don't seem to care about undefined behavior as long as their code don't produce an error and looks like working. They looks like the shift maintenance manager caused this incident.
C functions or types often have pointer types and you need to know how systems work for using pointers safely. Also, you need to read a manual or documents to use a libraries correctly. Documents for C libraries suppose reader knows about C lang.
Learning C lang might takes long time, but you might waste much longer times for debugging or asking a question and wait for response (and might not get the answer) if you use C functions without learn C lang and lower level things.
You can set the calling conversion of a proc by attaching a pragma to it.
proc MiniDumpCallback(CallbackParam: PVOID, CallbackInputParam: PMINIDUMP_CALLBACK_INPUT, CallbackOutputParam: PMINIDUMP_CALLBACK_OUTPUT): BOOL {.stdcall.} =
# ...