I'm looking for a simple but complete example of creating a VB.Net
ActiveX / COM component from a C Library. One should be able to use it
by calling the Server.CreateObject method from an ASP script. I have
already figured out how to do most of it, but I'm stumped on the part
where I call it from an ASP script.
Assume I have a VS.Net solution with three projects:
1. CLibrary - generates a DLL from the following C source code:
extern __declspec(dllexport) int authenticate_user(char *login, char
*password);
extern __declspec(dllexport) int authenticate_user(
char *login, char *password) {
return strcmp(login,password); // just compare the username and
password for demo purposes
}
then
2. ComClass - a VB.NET class that uses DLLImport to call the CLibrary.
I have enabled Interop in the project settings. Here's the source:
Public Class Class1
<DllImport("CLibrary.DLL", EntryPoint:="authenticate_user", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function
AuthenticateUser(<MarshalAs(UnmanagedType.LPStr)> ByVal Username As
String, <MarshalAs(UnmanagedType.LPStr)> ByVal Password As String) As
Int16
End Function
End Class
3. The above projects compile fine and work correctly if I call them
from a third project, ConsoleApplication1, a VB.NET Console
Application, which references ComClass:
Sub Main()
Dim objCom As New ComClass.Class1
Dim Username As String
Dim Password As String
Do Until False
Console.Write("Username: ")
Username = Console.ReadLine()
If Username = "" Then
Exit Do
End If
Console.Write("Password: ")
Password = Console.ReadLine
Console.WriteLine(objCom.AuthenticateUser(Username ,
Password))
Loop
objCom = Nothing
End Sub
the above works. However, I have two questions/concerns:
1. Any time I change the C Library, I have to physically copy the DLL
generated from CLibrary to the bin directory of the ConsoleApplciation
application. I would prefer to simply reference it in the
ConsoleApplication or ComClass project, but it won't let me do that
since it's not a .NET Assembly. Is there any way to automate that step?
2. I need to call ComClass from an ASP script. I tried using
RegSvr32.exe to register it but it tells me it can't find the
DLLRegisterServer entry point. How do I create a DLLRegisterServer
entry point, or is there a different way to register it? I'd like to be
able to call it via code such as:
sUsername = Request("Username")
sPassword = Request("Password")
Set oComClass = Server.CreateObject("ComClass.ComClass")
iResult = oComClass.AuthenticateUser(sUsername, sPassword)
Set oComClass = Nothing
Response.Write ("Result: " & iResult)
What do I need to do to be able to use the class from an ASP script?