470,647 Members | 1,229 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,647 developers. It's quick & easy.

Dynamicaly load and call a function in Win32 dll

Is it possible to dynamicaly to local and call a function in Win32 dll(not a
..net assembly dll) in C# at run time, for example, a C# program popup a
dialogbox to let use input which Win32 dll to be loaded, which function to
be called, and what are the parameters to call the function.
Thank you very much!
Jul 21 '05 #1
4 2669
Use reflection namespace for defining class and methods using data from your
dialog, compile it, load assembly and call.

HTH
Alex

"ZhangZQ" <zh*******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Is it possible to dynamicaly to local and call a function in Win32 dll(not a .net assembly dll) in C# at run time, for example, a C# program popup a
dialogbox to let use input which Win32 dll to be loaded, which function to
be called, and what are the parameters to call the function.
Thank you very much!

Jul 21 '05 #2
Alex,

Can you show me some sample code?
Thank you very much!

ZhangZQ
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:OA**************@TK2MSFTNGP09.phx.gbl...
Use reflection namespace for defining class and methods using data from your dialog, compile it, load assembly and call.

HTH
Alex

"ZhangZQ" <zh*******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Is it possible to dynamicaly to local and call a function in Win32 dll(not
a
.net assembly dll) in C# at run time, for example, a C# program popup a
dialogbox to let use input which Win32 dll to be loaded, which function

to be called, and what are the parameters to call the function.
Thank you very much!


Jul 21 '05 #3
> Is it possible to dynamicaly to local and call a function in Win32 dll(not
a
.net assembly dll) in C# at run time, for example, a C# program popup a
dialogbox to let use input which Win32 dll to be loaded, which function to
be called, and what are the parameters to call the function.


a partial solution is to write another Win32 dll that has fixed interface
and can dynamically load and call a function from Win32 library given by
name.

Regards,
Wiktor
Jul 21 '05 #4
You can start from
http://msdn.microsoft.com/library/de...dedomgraph.asp.

It's dynamic compilation topic. If you don't like this way, you can create
source code in any other suitable for you way - as file - and use csc.exe or
vbc.exe to make dll or exe, which will expose Win32 call to .net.

For every Win32 function you want to call in such way you need to know all
the details for DllImport attribute. In simple cases it is just what is the
type of returned result and what are the types of parameters to pass.

E.g. standard wrapper for SetWindowText might look like:

class Win32Wrapper {
public Win32Wrapper() {
[DllImport("User32")]
internal static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
...
public int Execute(IntPtr hWnd, int nCmdShow) {
return ShowWindow(hWnd,nCmdShow);
}
}
}

After you compile this, you can use Reflection to load resulting dll,
instantiate Win32Wrapper and call Execute with your parameters.

The rest is up to you. Which functions you want to define, which calls etc.

You can use similar approach when wrapper assembly is created in C++ 5 or 6,
or even VB6. Older style assemblies will require additional step - check
type library importer - tlbimp.exe. There is description what to do if you
want to change MSIL in assembly even, e.g.
http://msdn.microsoft.com/msdnmag/is...tprofilingapi/ - if you
really want to make this toy 100% dynamic and know how to modify IL.

Anyway, I think you should do your homework first. Copy/paste approach here
won't work really. For example, in this sample - how you plan to ask user to
specify hWnd parameter? Did you think about it already?

HTH
Alex

"ZhangZQ" <zh*******@hotmail.com> wrote in message
news:ux**************@TK2MSFTNGP09.phx.gbl...
Alex,

Can you show me some sample code?
Thank you very much!

ZhangZQ
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:OA**************@TK2MSFTNGP09.phx.gbl...
Use reflection namespace for defining class and methods using data from your
dialog, compile it, load assembly and call.

HTH
Alex

"ZhangZQ" <zh*******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Is it possible to dynamicaly to local and call a function in Win32 dll(not
a
.net assembly dll) in C# at run time, for example, a C# program popup a dialogbox to let use input which Win32 dll to be loaded, which

function to be called, and what are the parameters to call the function.
Thank you very much!



Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by jmdeschamps | last post: by
3 posts views Thread by lallous | last post: by
1 post views Thread by Andrew Moore | last post: by
9 posts views Thread by John Kirksey | last post: by
1 post views Thread by Mythran | last post: by
5 posts views Thread by Paul Cheetham | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.