I have a class that has methods like:
[DllImport("wfdb.dll")]
private static extern int setsampfreq(double freq);
The wfdb.dll file isn't in the current directory nor on the path ... so I
want to qualify it with the path name, for example:
[DllImport(@"c:\foo\wfdb.dll")]
However I don't know until run-time what the path is, so I want to set this
path in the static constructor for my class.
I can use class_type.GetMethods() to get MethodInfo[] and
method_info.GetCustomAttribute() to find the existing DllImportAttribute
instance for each method, but I can't change the DLL filename using
dll_import_attribute.Value because the Value property is read-only: the
filename can be defined only when then DllImportAttribute instance is
constructed.
I therefore want to create a DllImportAttribute instance at run-time and
associate it with the method. Unfortunately I don't see how to associate a
new attribute with an existing method at run-time.
Can this be done?
Or, can I do it only by creating the class and its methods from scratch, by
using AssemblyBuilder and TypeBuilder and MethodBuilder?
Mattias Sj?gren - 09 Jul 2004 16:03 GMT
Christopher,
>Unfortunately I don't see how to associate a
>new attribute with an existing method at run-time.
>
>Can this be done?
No. But you can do this
http://www.dotnetinterop.com/faq/?q=LoadLibrary
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Daniel - 10 Jul 2004 01:22 GMT
You could also use Reflection Emit I'm pretty sure.
Dan
> Christopher,
>
[quoted text clipped - 8 lines]
>
> Mattias
Shri Borde [MS] - 10 Jul 2004 00:17 GMT
You could define a dummy IL function called setsampfreqWrapper() which does
nothing. At runtime, you could define the NDirect method setsampfreq with
the right DllImport attribute, and also change the IL for
setsampfreqWrapper() using the profiling API to call the newly defined
NDirect method setsampfreq().
This is just one way to do it. I am not whether this is the most ideal
solution in this case.
Shri Borde [MSFT]
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
> From: "Christopher Wells" <christopher_wells@invalid.invalid>
> Subject: Set DllImportAttribute at run-time?
[quoted text clipped - 6 lines]
> Message-ID: <Od6FivbZEHA.2844@TK2MSFTNGP12.phx.gbl>
> Newsgroups:
microsoft.public.dotnet.framework.interop,microsoft.public.dotnet.framework.
clr
> NNTP-Posting-Host:
cpe00b0d027284c-cm014340008855.cpe.net.cable.rogers.com 65.50.47.140
> Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
> Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.clr:11118
microsoft.public.dotnet.framework.interop:25095
> X-Tomcat-NG: microsoft.public.dotnet.framework.clr
>
[quoted text clipped - 26 lines]
> Or, can I do it only by creating the class and its methods from scratch, by
> using AssemblyBuilder and TypeBuilder and MethodBuilder?
Christopher Wells - 11 Jul 2004 12:46 GMT
Thank you. Googling for "NDirect" eventually led me to
http://www.msjogren.net/dotnet/eng/samples/dotnet_dynpinvoke.asp
Junfeng Zhang[MSFT] - 13 Jul 2004 19:27 GMT
http://blogs.msdn.com/junfeng/archive/2004/07/14/181932.aspx
Consumed linked provided in this thread, and added .net 2.0 way for doing
so.
> I have a class that has methods like:
>
[quoted text clipped - 24 lines]
> Or, can I do it only by creating the class and its methods from scratch, by
> using AssemblyBuilder and TypeBuilder and MethodBuilder?