Funkcja z biblioteki z parametrem typowo C++ oraz jej event loop.

0

Mam funkcję z biblioteki:

int __stdcall _DGTDLL_RegisterScanFunc (int __stdcall (*scanfunc) (char*));

Próbuje ją otworzyć w C# (LoadLibrary, GetProcAddress, delegate...). Teraz chodzi tutaj o ten parametr. W jaki sposób to obłużyć

Dla przykłady bezparametrową funkcję z tej biblioteki wywołuje bez problemu.

int __stdcall _DGTDLL_GetVersion();

Kolejna część problemu. Mam opis funkcji RegisterScanFunc:

Description: use this to let the DLL know which function to call when it fires a scan event.

Do tego dochodzi problem, jako że ta funkcja przyjmuję jako parametr funkcję która ma się wywołać i w przykładzie z dokumentacji mam:

// now the DLL is properly loaded and initialized
if (registerScanFunc!=nullptr) {
registerScanFunc(&myScanCallback);
// From now on, myScanCallback is called when new scans come in
}
///////////// register other callback functions
///////////// your stuff here, keep in mind that you still need an event loop

I pytanie jak powinno w .net wyglądać event loop.

Nie bardzo wiem jak to opisywać, ale jak coś to pytajcie.

0
using System;
using System.Runtime.InteropServices;

class Program
{
	[UnmanagedFunctionPointer(CallingConvention.StdCall)]
	delegate int ScanFunc(string s);

	[DllImport("delele.dll")]
	static extern int  _DGTDLL_RegisterScanFunc(ScanFunc func);

	int Callback(string s)
	{
		Console.WriteLine("callback: {0}", s);
		return 42;
	}
	void Main2()
	{
		_DGTDLL_RegisterScanFunc(Callback);
	}
	static void Main()
	{
		new Program().Main2();
	}
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1