[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

Dynamicaly load and call a function in Win32 dll

ZhangZQ

10/31/2003 2:32:00 AM

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!


5 Answers

ZhangZQ

10/31/2003 4:23:00 AM

0

Alex,

Can you show me some sample code?


Thank you very much!

ZhangZQ


"AlexS" <salexru2000NO@SPAMsympaticoPLEASE.ca> wrote in message
news:OAy59P2nDHA.2456@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" <zhangzq71@hotmail.com> wrote in message
> news:%23JbWAd1nDHA.1096@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!
> >
> >
>
>


AlexS

10/31/2003 7:04:00 AM

0

Use reflection namespace for defining class and methods using data from your
dialog, compile it, load assembly and call.

HTH
Alex

"ZhangZQ" <zhangzq71@hotmail.com> wrote in message
news:%23JbWAd1nDHA.1096@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!
>
>


Wiktor Zychla

10/31/2003 9:11:00 AM

0

> 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


AlexS

10/31/2003 7:51:00 PM

0

You can start from
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcongeneratingsourcecodecompilingprogramfromcodedo....

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/issues/03/09/netpro... - 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" <zhangzq71@hotmail.com> wrote in message
news:uxfU4a2nDHA.1072@TK2MSFTNGP09.phx.gbl...
> Alex,
>
> Can you show me some sample code?
>
>
> Thank you very much!
>
> ZhangZQ
>
>
> "AlexS" <salexru2000NO@SPAMsympaticoPLEASE.ca> wrote in message
> news:OAy59P2nDHA.2456@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" <zhangzq71@hotmail.com> wrote in message
> > news:%23JbWAd1nDHA.1096@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!
> > >
> > >
> >
> >
>
>


mikegreonline

11/1/2003 12:23:00 AM

0

I may be misunderstanding the question, but it sounds like you want to use
PInvoke. It allows you to call Win32 DLLs from managed code. The DLL is
loaded when the code that references it is run. If you want to have more
control over when the DLL is loaded, you could create an unmanaged DLL that
does the loading of the DLL and call it from your managed code.

I hope this is helpful,

Thanks,

Michael Green
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only.