Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Set DllImportAttribute at run-time?

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?

Christopher Wells
Friday, July 09, 2004

How about just adding the path to the dll to the PATH environment variable? Rather than pollute the global environment you could have a stub loader that appends the path to the DLL to PATH via ProcessStartInfo.EnvironmentVariables and then invokes the real program via Process.Start(  ).

Duncan Smart
Friday, July 09, 2004

Someone has pointed me to http://www.dotnetinterop.com/faq/?q=LoadLibrary

Christopher Wells
Friday, July 09, 2004

And to http://www.msjogren.net/dotnet/eng/samples/dotnet_dynpinvoke.asp

Christopher Wells
Sunday, July 11, 2004

No. Do you even know what syntax errrors are? What that gooblygook  mess you just wrote?

Why do you need to "get" a method?  Teh method - actaully, the function- should alreday be a memeber of the class - where;s the need to "get" one? You get a pen that someone throws in the woods at night for being a moron and saying something stupid, like when I was in da service.

Correct sytax example:

CDialog::OpenDialog()
// .. do someting

Ta -da. Simple as that. 

Master of the universe
Friday, August 06, 2004

... and why do you need or even want a getMethod[] - I assume that's an array. Array of what ? What is the use for that? Some kind of "unoffical" version of OOP? Havent you embarrassed yourseves enough already with this bullshit?

Go fuck a calculator, and come back when you figure out what 2+2 equals.  If you do that, then print it. Else. try again. Proof counts.

Stupid.

Master of the universe
Friday, August 06, 2004

*  Recent Topics

*  Fog Creek Home