Tutorial :Programmatically generate a C Win32 DLL



Question:

I need to repeatedly generate a Win32 DLL with a registration information function. This function uses literals to return customer specific registration information, with a separate DLL being built per customer.

I have a test version working correctly, with hard-coded information for one customer. The urgency for some sites dictates I generate some DLL's manually, but I would like to give the client an application that dynamically emits the C source and builds a DLL on demand.

What would be the the best way to do this? I have VS 2008 C++ Express, and thus the cl.exe compiler. My current approach would simply be to have a C# application with a string constant for the C source, and before generation, replace tokens in that with required parameters, then build and link by shelling out and running cl.exe.


Solution:1

Define the literals as #defines and then all you need to do is generate a header file. An automated build system will take care of the rest.


Solution:2

Put all customer information as string resources in a .rc file. Link the corresponding .res file into your DLL. All code in the DLL that depends on that customer information would call LoadString to fetch this.

Then build a seperate program (or function) called "UpdateDLL.exe" that uses the Win32 APIs: BeginUpdateResource, UpdateResource, etc... to update the DLL with the new information.

Ship the following:

  1. A pre-compiled DLL that has empty (or default) strings for the customer information in the resources.

  2. Your UpdateDLL.exe tool that takes the DLL name and customer info file as command line param.

  3. Your customer runs "UpdateDLL.exe customer.dll myinfo.txt" to update his copy of the DLL with his information.


Solution:3

My generic idea is to have a program like this:

string UserName = "PLACEHOLDER UserName                     ";  string RegCode  = "PLACEHOLDER RegCode                      ";  bool CheckRegistration(string UserName, RegCode) {    ...  }  

Compile this to a .dll file. For each user, load the .dll file, find the two PLACEHOLDERs in it, and replace them the real user data. Make sure you pad the string with spaces.


Solution:4

The .NET tools (notably compilers) are included with the .NET Framework install. If you can avoid needing the DLL to be native Win32 then using Code DOM and framework support to create a managed DLL dynamically.

To bridge from Win32 to .NET, COM is the easiest approach, but this requires registration. To avoid your dynamic dll needing registration, create a bridge managed dll which exposes a COM interface, distribute this with your application and register it on install. It loads (and could also create) the dynamically created dll.

This way you avoid needing to ensure the user has the Win32 SDK installed (or other route) for cl.exe and link.exe.


Solution:5

I assume that most of the DLL you need to generate is exactly the same and the only thing that changes is the per-customer data which can be say, a string. If this is the case then the easiest way to do this is to add to in your code a big enough string array and fill it with some known and unique pattern. say something like "MYSTART-------------MYEND" and compile the DLL.
To generate the per-client DLL, you don't have to actually recompile the DLL, only to replace that unique string with the per-client string. You need to make sure that the string you replace is the same exact size as the one you added in your code so that you don't add any bytes to the file.
You can do this search and replace manually using a hex editor or using a 3-line python/perl script.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »