Accessing DLL files...

B2Ben

Well-known member
Joined
Aug 17, 2006
Messages
52
Programming Experience
Beginner
Hey all...

I'm totally new to dealing with DLL files, and I want to build a utility in VS2003 to connect to various DLL files.

(as a side note, if anyone knows any good tutorials for making/using DLLs, I'd love to see it :) )

A little background...
My company creates custom DLL files that contain settings for customizing our software. If we want our software to act differently, we just drop in a different DLL with different settings stored in it.

I'd like to make a utility to read the settings in a particular DLL file, which may not be in the standard file location(s) for DLL files.

So, for example, the DLL file has a function inside of it that I want to run:
GetOption( OptionName as String ) as String
This would look up the option name, and return it to me as a string.

In my VB code, I would do something like this, right?
VB.NET:
Public Declare Function GetOption Lib "MyDLLFile.dll" (ByVal OptionName as String) as String

What I want to do is be able to choose a particular DLL file with an OpenFileDialog, and change "MyDLLFile.dll" to the full path of another DLL file - one that might be on my desktop, or in a network folder. I tried replacing "MyDLLFile.dll" with a variable containing a path, but Visual Studio didn't like that.

I hope that all made sense... any help would be greatly appreciated.
 
Have you tried it with the new style declare...

Imports System.Runtime.InteropServices.


VB.NET:
<DllImport(Path)> _
Public Shared Function GetOption(Byval optionName As String) As String
End Function

On the other thing, creating .dll files is easy. Just set your project as a class library project and, bingo. There's your .dll when you compile.

As for settings. I'm not sure a .dll is a good place to store them. Wouldn't they be better stored in an .appconfig or even just standard .xml file.
 
OK... is that a .NET 2 thing? I have VS2005 at home, but I use VS2003 at work. Forgive my ignorance, but could you show me a more detailed example on what you did?

As for storing settings... it's more complicated than that... When a customer buys our software, they pay for certain features. The DLL turns their chosen features on or off. We don't want the customer to be able to open a config file and activate a feature they didn't pay for... this is why we lock them up in a DLL file.
 
Dim Path as string = "C:\folder\file.dll"
<DllImport(Path)>

VS2003 produces an error:
"Constant expression is required" (referring to using Path in the DllImport function)
 
Ok, i had a feeling that would happen. But don't worry as i've thought of a possibility..

If you create a file named Something.vb and put it somewhere safe. In that file put your declare function...

Public Declare Function GetOption Lib * (ByVal OptionName as String) as String

But leave out the name of the .dll and just put the asterisk. Then when you know the name of the .dll inside yuor executable you can open this file write the string of the .dll in place of the asterisk. Then use the CodeDom namespace to compile the code on-the-fly and execute the statement.

I think that just may work.:)
 
Crazy... compiling code on the fly... this should be fun.

While I have your attention... got any examples of compiling/running code on the fly? :)
 
Loads of examples all over the web. Here's one at the code project..

http://www.codeproject.com/vb/net/DotNetCompilerArticle.asp


I'm not sure how efficient it will be. At a guess it's not going to be lightning fast, but then we are only talking milliseconds here. Also when you get it to work it will be quite a cool thing for your app to do.:)
 
Another approach is to look at sending a text file that would unlock parts of the app.

For instance, If you sent a text file with the contents 'Option1;Option2;Option4' etc

Your program could read the content of that file and enable features as appropriate.

AHA! I hear you say. 'The user could edit that file to enable features!'

That's where the next part comes in: Encrypt it.
VB.net has godd built in encryption, so your ouput would look more like 'jYFyFyFThfHft^&F7D' to the end user, but your program could decrypt it and make sense of it.

Without your encryption key, their chance of guessing the string is virtually null.
 
Ok, i had a feeling that would happen. But don't worry as i've thought of a possibility..

If you create a file named Something.vb and put it somewhere safe. In that file put your declare function...

Public Declare Function GetOption Lib * (ByVal OptionName as String) as String

But leave out the name of the .dll and just put the asterisk. Then when you know the name of the .dll inside yuor executable you can open this file write the string of the .dll in place of the asterisk. Then use the CodeDom namespace to compile the code on-the-fly and execute the statement.

I think that just may work.:)

I got it... it works like a charm!

I created the code to be executed by the run-time compiler, and just made it into an embedded resource. At run-time, I extract the text from the embedded file, do a search & replace to change the path of the DLL file, compile it, and run my function. Works great!

Thanks for the help, vis781
 
Back
Top