持有DLL全局范围变量的问题 [英] Problme in holding the dll global scope variable

查看:78
本文介绍了持有DLL全局范围变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人

我在将dll加载到c#窗口应用程序中时遇到问题,我的问题是关于在加载和工作时保持dll的状态,请查看代码

every body

i have a problem in loading a dll into c# window app program, my problem is about holding the state of dll when it is loaded and working, please look at code

public const string DllName = "c:\\dllname.dll";
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllName);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr dllHandler, string functionName);

[DllImport("kernel32.dll")]
public static extern IntPtr FreeLibrary(IntPtr dllHandler);

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int SetConnectPort(Int16 devicePort);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int OpenDeviceEx(ref string pName, UInt32 dwFlags);

IntPtr dll = NativeSupport.LoadLibrary(DllName);

IntPtr setConnPortAddrs = GetProcAddress(dll, "SetConnectPort");
SetConnectPort setConnectPort = (SetConnectPort)Marshal.GetDelegateForFunctionPointer(setConnPortAddrs, typeof(SetConnectPort));


                

IntPtr dll = LoadLibrary(NativeSupport.DllName);

IntPtr setConnPortAddrs = GetProcAddress(dll, "SetConnectPort");
SetConnectPort setConnectPort = (SetConnectPort)Marshal.GetDelegateForFunctionPointer(setConnPortAddrs, typeof(SetConnectPort));

IntPtr openDeviceAddrs = GetProcAddress(dll, "OpenDeviceEx");
OpenDeviceEx openDevice = (OpenDeviceEx)Marshal.GetDelegateForFunctionPointer(openDeviceAddrs, typeof(OpenDeviceEx));

int res = setConnectPort(8);                

string ip = textBox1.Text.Trim();
res = openDevice(ref ip, 0x00000001);



在代码中,我使用功能(SetConnectPort)设置dll的状态以通过tcp/ip
连接到打印机设备
通过usb连接设备没有问题,一切正常,但是通过ip功能连接无法连接到设备.
我认为函数SetConnectPort可以正常工作,但是在下一条语句中,由SetConnectPort引起的变量或更改丢失,并且OpenDevice无法正确完成其工作(找不到设备,因为默认情况下它使用USB连接) .

实际上,上面的代码将dll加载到内存中,并且它的地址在dll中,并且必须对SetConnectPort的操作进行一些更改,以更改dll使用的内存.
我更改了CallingConventions,并且在工作上没有区别

我该如何解决这个问题?



in the code i use function (SetConnectPort) to set the state of dll to connecting to printer device via tcp/ip

in connecting the device via usb there is no problem and every things works fine, but in connecting via ip functions can not connect to device .
i think that the function SetConnectPort works fine but in next statement the variables or changes that caused by SetConnectPort is lost and OpenDevice can not done it''s job correctly (can not find the device, because by default it is using the usb connection).

Actually the above code loads dll into memory and it''s address is in dll and the action of SetConnectPort must be caused some changes it the memory that is used by dll.
i changed of CallingConventions and it is not differed in working

how can i fix this problme ?

推荐答案

首先,您对SetConnectPortOpenDeviceEx可能故障的解释是错误的.如果加载了DLL,它的行为就像一个正常的可执行模块,其中保留了所有静态内存,因此您可以考虑在执行过程中保留其内部状态(如果有的话).没有什么要照顾的.

目前尚不清楚为什么要使用LoadLibraryGetProcAddress获得对这两个功能的访问.您可以像使用LoadLibraryGetProcAddress自己一样完全调用/调用它们.这将是更可靠的方法.

最后,如果您拥有可以通过TCP进行操作的打印机,则可以通过System.Net.Sockets.TcpClient完全使用它. sockets.tcpclient.aspx> http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx [
First of all, your explanation of possible malfunction of SetConnectPort and OpenDeviceEx is wrong. If a DLL is loaded, it behaves like a normal executable module with all its static memory preserved, so you can consider that its internal state, if any, is preserved during execution. There is nothing you should take care of.

It is not clear why would you obtain the access to these two functions using LoadLibrary and GetProcAddress. You could P/Invoke them exactly as you did it with LoadLibrary and GetProcAddress themselves; and it would be more reliable approach.

Finally, if you have a printer that can work via TCP, you could fully use it through the class System.Net.Sockets.TcpClient, http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx[^]. It would not require P/Invoke at all and be fully portable, work on any OS.



I assumed it''s given, but maybe it won''t hurt to remind you, that is a standard printer device, you could install it in the system in a standard way and use it in your applications as any other printer.

—SA


这篇关于持有DLL全局范围变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆