How to use NewLazyDLL (golang) to custom dll export from c# to set parameters

0

I wrote a custom dll export in c# which is working in c

This is my c# code for dll export

[DllExport]
/* GetMSLResult from the parameter as lat, lon, and gps elevation */
public static double GetMSLResult(IntPtr lat, IntPtr lon, IntPtr gpsElevation) => Helper.GetMSL(Helper.GetGeoID(Helper.IntPtrToDouble(lat), Helper.IntPtrToDouble(lon)), Helper.IntPtrToDouble(gpsElevation));

which is working and expose via

dumpbin /exports ClassLibrary1.dll

Which has my procedure is present

Also as i test in c code its working as well this is my

#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

typedef int(*GetMSLResult)(const wchar_t* str, const wchar_t* str1);

int main()
{
    auto dll = ::LoadLibrary(L"ClassLibrary1.dll");   // さっき作ったC# DLLの名前を指定する
    {
        auto result = reinterpret_cast<GetMSLResult>(::GetProcAddress(dll, "Count"));
        wcout << result(L"34.00", L"40.00") << endl; // this returns 74.00
        wcout << result(L"90.00", L"40.00") << endl; // this returns 130.00


    }
    if ( dll )
        ::FreeLibrary(dll); // 解放は忘れずに!!

    return 0;
}

this is working successfully, in golang i created a test code to read the exported dll not imported dll.

I wrote a simple test in windows how to do it but it seems i got an error and exited when i try to call the function i want to return the value

package main

import (
    "fmt"
    "syscall"
    "unsafe"
)

func main() {
    h := syscall.NewLazyDLL("ClassLibrary1.dll")
    proc := h.NewProc("GetGeoID")

    a := string("32.00")

    n, _, _ := proc.Call(uintptr(unsafe.Pointer(&a))) 

    //fmt.Printf("teast  %v", n)
    fmt.Printf("Hello dll function returns %d\n", n)
}

Currently this is my golang code.

In my GeoID expected two parameter which is lattitude and longitude

so in my code in golang

a := string("32.00")
b := string("32.00")

i want this to pass the parameter a and b to my dll procedure functions
so that i could be able to get a result.

n, _, _ := proc.Call(uintptr(unsafe.Pointer(&a))) 

But currently i received this error

Exception 0xe0434352 0x80131537 0x0 0x766afd62
PC=0x766afd62

syscall.Syscall(0x6e35290e, 0x1, 0x11006108, 0x0, 0x0, 0x0, 0x0, 0x0)
        C:/Go/src/runtime/syscall_windows.go:184 +0xbb
syscall.(*Proc).Call(0x11004100, 0x110120e0, 0x1, 0x1, 0x10, 0x4907e0, 0x1, 0x110120e0)
        C:/Go/src/syscall/dll_windows.go:171 +0x10a
syscall.(*LazyProc).Call(0x11049aa0, 0x110120e0, 0x1, 0x1, 0x0, 0x44bc01, 0x11034000, 0x11034000)
        C:/Go/src/syscall/dll_windows.go:333 +0x48
main.main()
        C:/Users/christopher/developer/go/src/gotest/main.go:16 +0x103
eax     0x19fbc0
ebx     0x5
ecx     0x5
edx     0x0
edi     0x1
esi     0x19fc80
ebp     0x19fc18
esp     0x19fbc0
eip     0x766afd62
eflags  0x216
cs      0x23
fs      0x53
gs      0x2b
exit status 2

Is there any good way i can call my custom dll procedure or function like GetAge(34, 56) and the result in golang? currently ive been using LoadDLL, NewLazyDLL and other stuff but i could not be able to achieve my goal, does somebody can suggest a good way to do it?

windows
go
dll
ffi
asked on Stack Overflow Jul 11, 2019 by Yabetsu2018 • edited Jul 11, 2019 by kostix

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0