Pointers to Functions in C#

G

Guest

I'm trying to register/unregister a COM DLL from the C# code, basically to
emulate Regsvr32.exe. Using P/Invoke I call LoadLibrary for the DLL and when
that succeeds I call GetProcAddress with the second parameter
"DllRegisterServer". So now I have an address to exported function. However I
can't find a way to call this function from C#. I tried to use unsafe code to
declare a pointer to a function which would not even compile. I played with
delegates with not results. Thanks for any ideas. Kris.
 
G

Guest

Mattias,
Thank you very much for your help. Your suggested solution works great. I
knew Reflections were very powerful but this is mind boggling. I have lots to
learn.
Regards,

Kris.
 
G

Guest

Mattias,
this is Kris again. Everything works fine with one minor problem. If I try
to register DLL which depends on another DLL and this DLL is missing the
message box pops out from somewhere. I get the same message box also if I
call LoadLibrary() method from Win32. After I acknowledge the message box
then later I get an exception from your code and that's great. Is there any
way to avoid getting the message box? Any other function to call first to
check the dependency list? Thanks,
Kris.
 
G

Guest

Mattias,
Thank you very much again. I finally got rid of the message box. The
parameter I actually used was SEM_FAILCRITICALERRORS. Thanks for all your
help,
Regards, Kris.
 
G

Guest

Mattias,
This is Kris again. Your code properly registers/unregisters DLLs. However
if later I try to remove any of the DLLs that were unregistered, still from
withing the same program (or to remove a directory where they reside ) I get
an exception: Exception deleting directory: Access to the path "XXX.dll" is
denied. If the program is still running and I try to remove any of the DLLs
manually from Windows Explorer I get a message box: Cannot delete: Access is
denied. The source file may be in use. Every DLL that was unregistered is
still in use! Apparently the DllRegServer class is still holding a reference
to every DLL file that was unregistered. I tried several things I could think
of, even calling Garbage Collector explicitly but the problem persists until
I exit the program. I wonder if you have any thougths on that? Regards,
Kris.
 
G

Guest

Mattias,
For some reason I didn't even think about it. I was trying to do it all in
managed code. By using Win32 API GetModuleHandle() and FreeLibrary() I was
able to make it work. Thank you again,
Kris.
 
G

Guest

Mattias,
This is Kris again. I'm really putting your DllServerReg class to test. Now
I found that if I unregister the DLL and then do some other things (such as
remove it and replace it with a new one) and then try to register is again I
get an exception from you class: "Exception has been thrown by the target of
an invocation". I get the same exception if I try to unregister or register
the same DLL twice. I always create new DllServerReg class for each DLL being
unregistered/registered. I call my GetModuleHandle() and FreeLibrary()
methods. I always get the exception the second time I try to do something
with the DLL. I need to quit and restart my program to make it work again.
Any thoughts?
Kris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top