Click here to Skip to main content
15,902,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, 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

C#
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);


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 ?
Posted
Updated 20-Feb-12 1:06am
v3
Comments
MarqW 20-Feb-12 10:22am    
Rather than use LoadLibrary, could you not use the DllImport attribute to import your DLL's functions?

1 solution

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.

[EDIT]

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
 
Share this answer
 
v3
Comments
Areff 21-Feb-12 2:08am    
you absolutely right , the problem is clear to me now because i found that the infrastructure of network doesn't works fine , and my approach to loading the DLL using GetProcAddress, LoadLibrary is OK, and i tested your solution the using of P/Invoke will works fine too. tanks for your help.
Sergey Alexandrovich Kryukov 21-Feb-12 3:21am    
That is so great that you understood things properly and got it so quickly. This is a pleasure to write to a well understanding inquirer.

Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900