473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pointers to Functions in C#

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.
Nov 16 '05 #1
9 1525
Thanks for any ideas.


Here's one solution:

http://www.msjogren.net/dotnet/eng/s...dynpinvoke.asp


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
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.

"Mattias Sjögren" wrote:
Thanks for any ideas.


Here's one solution:

http://www.msjogren.net/dotnet/eng/s...dynpinvoke.asp


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #3
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.

"Mattias Sjögren" wrote:
Thanks for any ideas.


Here's one solution:

http://www.msjogren.net/dotnet/eng/s...dynpinvoke.asp


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #4
Is there any way to avoid getting the message box?


You can probably use SetErrorMode(SEM_NOOPENFILEERRORBOX).


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #5
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.

"Mattias Sjögren" wrote:
Is there any way to avoid getting the message box?


You can probably use SetErrorMode(SEM_NOOPENFILEERRORBOX).


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #6
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.
"Mattias Sjögren" wrote:
Thanks for any ideas.


Here's one solution:

http://www.msjogren.net/dotnet/eng/s...dynpinvoke.asp


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #7
I wonder if you have any thougths on that?


You can try calling FreeLibrary on the DLL.


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #8
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.

"Mattias Sjögren" wrote:
I wonder if you have any thougths on that?


You can try calling FreeLibrary on the DLL.


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #9
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.
"Mattias Sjögren" wrote:
I wonder if you have any thougths on that?


You can try calling FreeLibrary on the DLL.


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alex | last post by:
Is there any problem with sending function pointers through in a variable argument list? I have a function like the following: typedef (*ptr2FuncType1)( int ); typedef (*ptr2FuncType2)( double...
2
by: Thomas Matthews | last post by:
Hi, I would like to create a table (or vector) of pointers to templated functions. 1. How do I declare a typedef of a pointer to a templated function? For example, I have some functions...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
13
by: munni | last post by:
hi i want to write a program on function pointers where i have not written any programs using function pointers till now. i want to take an array of 26 functions which r to be pointed by this...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
15
by: Christian Christmann | last post by:
Hi, in which situations pointers to functions might be more efficient/convenient than a direct function call? In the example I've found so far, I see no advantage of using pointers to...
1
by: Bushido Hacks | last post by:
A private utility function using a function pointer sounds ideal to me. I want to simpify writing the same set of loops. While there are a few functions that can't use it, setting and modifying...
12
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
12
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair...
8
by: a | last post by:
Hello. Suppose I have a family of functions f_0(x) = 0*x f_1(x) = 1*x .... f_n(x) = n*x taking float and returning float (naturally, the actual functions would
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.