cで動作するカスタムdllエクスポートをc#で作成しました
これは、dll エクスポート用の私の C# コードです。
[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));
これは機能しており、経由して公開しています
dumpbin /exports ClassLibrary1.dll
Which has my procedure is present
また、Cコードでテストすると、これも機能します
#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;
}
これは正常に機能しています.golangで、インポートされたdllではなくエクスポートされたdllを読み取るテストコードを作成しました。
私はそれを行う方法をWindowsで簡単なテストを書きましたが、値を返したい関数を呼び出そうとするとエラーが発生して終了したようです
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)
}
現在、これは私のgolangコードです。
私のGeoIDでは、緯度と経度の2つのパラメーターが期待されていました
だから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)))
しかし、現在、私はこのエラーを受け取りました
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
GetAge(34, 56) のようなカスタム dll プロシージャまたは関数を呼び出して、golang で結果を得る良い方法はありますか? 現在、LoadDLL、NewLazyDLL、その他のものを使用していますが、目標を達成できませんでした。誰かがそれを行う良い方法を提案できますか?