dton37910 2019-07-11 08:19
浏览 699

如何使用NewLazyDLL(golang)自定义dll从C#导出以设置参数

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)
}

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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)