I.T. Discussion Community!
-Collapse +Expand
Search C# Group:

-Collapse +Expand C# To/From
-Collapse +Expand C# Study Test
-Collapse +Expand C# Store

Prestwood eMagazine

June Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC#WebForms Cod...   Print This    All Groups  
  From the November 2015 Issue of Prestwood eMag
C# WebForms Coding Tasks:
Using a Legacy Windows DLL in ASP.NET
Posted 11 years ago on 11/5/2009
Take Away:

This article will show how to access a function in a legacy COM dll from a ASP.NET web page. By legacy I mean a dll that is not a .NET managed code dll (a Win32 DLL). 

 Tags: DLL , 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 {
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);

More Info

Code:  [DllImport] attribute
FAQ:  Call a DotNet DLL from Win32 Apps
FAQ:  Using Win32 DLLs in .NET


Share a thought or comment...
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P112A1
Enter key:
KB Post Contributed By Bryan Valencia:

Bryan Valencia is the author of millions of lines of code and writes for PrestwoodBoards and Visual Studio Journey on-line magazines.

Visit Profile

 KB Article #102075 Counter
Since 11/5/2009

Follow PrestwoodBoards on: 

©1995-2020 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech