Creating a custom DLL

redbrad0

Member
Joined
May 31, 2007
Messages
14
Programming Experience
Beginner
I found some code that I need in my C# application but its written in VB.net so I was going to create a VB.net DLL and just call the functions in my C# application. So I copied the code into the DLL, compiled it, and added it as a reference to my C# application. The problem is that I can not seem to figure out how to tell VisualStudio I want to use this DLL with the "Using" comand (Example: using mydllfile;) Below is the entire code in my Class1.vb in my DLL project. Am I doing something wrong in creating this DLL that I can not seem to figure out how to reference it?

VB.NET:
Option Explicit On

Public Class Class1

    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal phkResult As Long) As Long
    Private Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long

    ' Return codes from Registration functions.
    Const ERROR_SUCCESS = 0&
    Const ERROR_BADDB = 1&
    Const ERROR_BADKEY = 2&
    Const ERROR_CANTOPEN = 3&
    Const ERROR_CANTREAD = 4&
    Const ERROR_CANTWRITE = 5&
    Const ERROR_OUTOFMEMORY = 6&
    Const ERROR_INVALID_PARAMETER = 7&
    Const ERROR_ACCESS_DENIED = 8&

    Private Const HKEY_CLASSES_ROOT = &H80000000
    Private Const MAX_PATH = 260&
    Private Const REG_SZ = 1

    Public Function AssociateExtension(ByVal Extension As String, ByVal ApplicationKey As String, ByVal ApplicationName As String, ByVal ApplicationPath As String) As Boolean
        Dim sKeyName As String 'Holds Key Name in registry.
        Dim sKeyValue As String 'Holds Key Value in registry.
        Dim ret& 'Holds error status if any from API calls.
        Dim lphKey& 'Holds created key handle from RegCreateKey.

        'This creates a Root entry called "MyApp".
        sKeyName = ApplicationKey
        sKeyValue = ApplicationName
        ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
        ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)

        'This creates a Root entry called .BAR associated with "MyApp".
        sKeyName = "." & Extension
        sKeyValue = ApplicationKey
        ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
        ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)

        'This sets the command line for "MyApp".
        sKeyName = ApplicationKey
        sKeyValue = ApplicationPath & " %1"
        ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
        ret& = RegSetValue&(lphKey&, "shell\open\command", REG_SZ, sKeyValue, MAX_PATH)

        Return True
    End Function
End Class
 
Well I got it included... so I am good on that, but I can not seem to call the function as after I select "Class1" it shows their are no functions available.
 
With VB.Net you would call it like this:
VB.NET:
using x as new classlibrary1.class1
  x.AssociateExtension()
end using
I can't help you with C# code, but you can for example run it through a converter http://labs.developerfusion.co.uk/convert/vb-to-csharp.aspx, or read the C# book, or ask the C# forum.

For that code you also don't need Win32 API calls, .Net Framework have classes to handle registry in Microsoft.Win32 namespace.
 
Back
Top