Pages

Monday, November 2, 2009

Using a Legacy Windows DLL in ASP.NET

Recently I had to use a very old DLL in an ASP.net web site. The dll has one function (that I care about) and I went down one rabbit trail after another looking for how to get this done. This is NOT a .NET managed dll, it's an old-style dll where there is a function that accepts various parameters and returns a horrible coded string.

Without much ado, let me show you what worked in this case. Note that Visual Web Developer was of NO HELP AT ALL when it came to figuring out the setup for this.

Place the dll. I found that it was nearly impossible to place the dll in my /bin/ directory under my website. Likewise I found that it was impossible to create a reference to it in the /bin/ directory. I placed the dll in my C:\Windows\System32\ directory. I have seen some chatter that this is the only place the IIS server has enough permissions for, so even if you think someplace else might be better, I'm telling you that this worked for me.

Wrap the function. You need to create a wrapper in your c# code to call the function in the dll. Note that you MUST have the function call specs - VWD will not find them for you like it will for a .NET managed dll.
Here's what the setup looks like... anything highlighted will have to be replaced with the info for your dll call.


using System;
using System.Runtime.InteropServices;

/// 
/// Wrapper for a function in the CodeGen DLL
/// 
public class CodeGen
{
 [DllImport("codegen.dll")]
 public static extern string CreateCode2(
  int level,
  string name,
  string encrypt_template,
  Int64 hardwareID,
  int otherinfo1,
  int otherinfo2,
  int otherinfo3,
  int otherinfo4,
  int otherinfo5
  );
}

Now you just add a call to your wrapper function from wherever it's appropriate.


 protected void Page_Load(object sender, EventArgs e)
 {
  string code = CodeGen.CreateCode2(1, 
     "Bob", 
     "This is an encrypting template", 
     0xface0fff, 0, 0, 0, 0, 0);
  Label1.Text=code;
 }

Share This!

Contact Us

Name

Email *

Message *