Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Another CodeDom Question

Hi -

Does anyone here know how (or if it's possible) to use CodeDom to declare a function that exists in an unmanaged dll?  (Like VB6's Declare statement.)

Thanks!

Aaron F Stanton
Tuesday, October 19, 2004

Don't you just emit a statement with the DllImport attribute? For example, to create a statement like this:

/// <summary>
/// Calls the foo function in bar.dll
/// </summary>
[DllImport("bar.dll")]
public static extern int foo(uint baz);

Christopher Wells
Tuesday, October 19, 2004

Hi -

Yeah, I think I can figure out how to generate the DllImport bit programmatically, but the extern piece is being a bit sticky.  (I see that's what has to be generated, just not sure how.)

Thanks!

Aaron F Stanton
Tuesday, October 19, 2004

http://www.msjogren.net/dotnet/eng/samples/dotnet_dynpinvoke.asp might be the thing (the TypeBuilder.DefinePInvokeMethod method).

Christopher Wells
Tuesday, October 19, 2004

Cool!  I will look into that.  Thanks for the link.

Aaron F Stanton
Tuesday, October 19, 2004

Hmm...seems that will emit assemblies with external declarations via Reflection.  I was hoping to find a way to do it via CodeDom.  I might wind up doing a mix, which is a bit unsatisfying.  Interesting that MS chose to have two separate models, one for source code and one for compiled code, and that the mapping between the two is incomplete.  (It seems that there is some language dependent stuff in CodeDom, which is fine with me - if a given language doesn't support a given construct, you obviously can't programmatically create that construct in that language.  It would be nice to be able to programmatically create every possible construct for a language that was created hand in hand with the CLR (C#).  Sigh.)

Aaron F Stanton
Wednesday, October 20, 2004

Yes, it is annoying that not all of the languages support all of the possible constructs available in IL.

You can understand it for third-party languages, but C#, come on guys. Also, I don't like the idea that VB.NET has stuff in it that C# doesn't.

Nemesis
Wednesday, October 20, 2004

Well, what's even more annoying than the language not supporting all of the features of IL is that there are features that they *do* support (extern being the most immediate example) that you can't programmatically generate via CodeDom.  I want to build the tree, hand it a CodeProvider, and have it vomit out reams of source in the language I desire, and if the language itself doesn't support it, fine, throw an exception for me to handle or something like that, but I should be able to generate the entire set of constructs the language can support.

</rant>

Sorry about that.  I think I'll just write my own damn CodeDom heirarchy.  Can I do a better job than MS?  Probably not, but I can learn from what I consider their mistakes, and I'll own the source code so that if I find a bug, I can fix it my own damn self.

Aaron F Stanton
Wednesday, October 20, 2004

*  Recent Topics

*  Fog Creek Home