Thanks to visit codestin.com
Credit goes to github.com

Skip to content

CLR load function not working in nim's c++ dll #91

@randomfox151617

Description

@randomfox151617

I have created a cpp dll using nim with an exported function which loads a dotnet dll from disk using winim/clr library's load function. If I pass file path to the load function, it works correctly and loads the dll (I have confirmed it by calling a function of loaded dll which pops a messagebox), but If I pass the byte array of file, the code execution gets stuck at load function call. Any reason why is it happening for this particular overloaded method ?

Example :
Working code with path as parameter to load function

import winim/clr

proc NimMain() {.cdecl, importc.}

proc DllMain(hinstDLL: HINSTANCE, fdwReason: DWORD,
    lpvReserved: LPVOID): BOOL {.stdcall, exportc, dynlib.} =
                NimMain()

                if fdwReason == DLL_PROCESS_ATTACH:
                   return true

proc abc(): void {.stdcall, exportc, dynlib.} =
                try:
                                var assembly = load("PATH_OF_DOTNETDLL")

                                MessageBox(0, "loaded", "loaded", 0)
                                var Program = assembly.GetType("DotNetDll.Program")
                                var programInstance = @Program.new()
                except Exception as ex:
                                MessageBox(0, ex.msg, "exception", 0)

In the above example, I am instantiating Program class of DotNetDll dll which contains a static constructor with a messagebox. the messagebox from the constructor is getting executed and working fine

Example : Not working with byte array loading

import winim/clr

proc NimMain() {.cdecl, importc.}

proc DllMain(hinstDLL: HINSTANCE, fdwReason: DWORD,
    lpvReserved: LPVOID): BOOL {.stdcall, exportc, dynlib.} =
                NimMain()

                if fdwReason == DLL_PROCESS_ATTACH:
                   return true

proc abc(): void {.stdcall, exportc, dynlib.} =
                                let file = "PATH_OF_DOTNETDLL"
				let f = file.open(fmRead)
				MessageBox(0, "read", "read", 0)
				let filesize = f.getFileSize()
				MessageBox(0, $filesize, "size", 0)
				var bytes = newSeq[byte](filesize)
				var read = f.readBytes(bytes, 0, filesize)
				MessageBox(0, $read, "read", 0)
				try:
					var assembly = load(bytes)
					MessageBox(0, "loaded", $read, 0)
					var Program = assembly.GetType("DotNetDll.Program")
					var programInstance = @Program.new()
				except Exception as ex:
					MessageBox(0, ex.msg, "exception", 0)

In the above example, I am not able to see the "loaded" message box and the execution gets stuck. it does not even throw any exception.

I am executing the dll with rundll32 exe, also cpp dll is x64 based and dotnet dll is made in .NET Framwork 2.0. I have also tested both the methods in a normal nim exe and it works fine. but it is not working in cpp dll as shown above

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions