locked
Using dll's in C#, convert this Tiny bit of vb6 code into C# [Beginner]

    Question

  • Someone made a dll [AresHash.dll] in VC++ & I want to use it in C#. He included the Visual Basic6 code for using the dll and it works well.. but now for me its C# and im new to using dll's here.....

    Here is the VB Code
    Private Type hash_channel
        externalIP As Long
        internalIP As Long
        port As Integer
        roomName As String
        topic As String
        VB As Boolean
    End Type
       
       
    Private Declare Function Decode Lib "AresHash.dll" (ByVal hash As String, ByRef hc As hash_channel) As Boolean

    Private Sub Test()
      Dim hc As hash_channel
      Dim ret As Boolean
      hc.VB = True
      ret = Decode(txtHash, hc)
      If ret = False Then
       MsgBox ("Error, not a valid hashlink")
       Exit Sub
      End If
      
      txtExtIP.Text = hc.externalIP
      txtIntIP.Text = hc.internalIP
      txtPort.Text = hc.port
      txtName.Text = hc.roomName
      txtTopic.Text = hc.topic
       
    End Sub


    Here is the C# code, [ I gave it a TRY]
    using System;
    using System.Text;
    using System.Runtime.InteropServices;
    namespace DefaultNamespace
    {
     public class UnManagedDll
     {
        [DllImport("AresHash.dll")]
        static extern bool Decode(string hash,ref hash_channel hc);
      public static void Main()
      {

       string hashlink = @"F5fJCV94LX2mAYheAN/H+47PoeKPF4RGbQOgHuW03t7K97HG1DpwT/sMsvZrdOb/K3sHMqcy7PTWWyo3m52CnYYMXpeTmDHz8Ps5rolnpMIWy+rXnXweHnE0mnFvx7KGG2pHjsg/3sIO4BJ5r9shcC4e3divwqgTaEyjoiMeUA7bYpNsI1DBtYQqAJf7D1NkXv8MajnqB8vziQFgGmk=";
        
       hash_channel hc = new hash_channel();
       bool ret;
       hc.VB = true;
       
       ret = Decode(hashlink,ref hc);
       Console.WriteLine(ret.ToString());
       Console.WriteLine(hc.roomName);
      }
      
      public struct hash_channel
      {
          public long externalIP;
          public long internalIP;
          public int port;
          public string roomName;
          public string topic;
          public bool VB;
         
      }
     }
    }

    But unfortunately my C# code does not work :(
    Can anyone correct my code or translate the code to C# from vb6?,
    To download the dll's and the vb code, go here http://atlas.walagata.com/w/soothsayer/Hashlink_Decrypter.zip

    Here is a sample hashlink you can use:

    F5fJCV94LX2mAYheAN/H+47PoeKPF4RGbQOgHuW03t7K97HG1DpwT/sMsvZrdOb/K3sHMqcy7PTWWyo3m52CnYYMXpeTmDHz8Ps5rolnpMIWy+rXnXweHnE0mnFvx7KGG2pHjsg/3sIO4BJ5r9shcC4e3divwqgTaEyjoiMeUA7bYpNsI1DBtYQqAJf7D1NkXv8MajnqB8vziQFgGmk=


    Awaiting a reply,
    Thx

    Friday, September 30, 2005 5:33 PM

Answers

  • The problem is that a VB6 "Long" is a VB.NET "Integer" and a VB6 "Integer" is a VB.NET "Short".

    Here is the translation (produced with our Instant C# VB to C# converter) after upgrading the VB6 code to VB.NET first:

    private struct hash_channel
    {
     public int externalIP;
     public int internalIP;
     public short port;
     public string roomName;
     public string topic;
     public bool VB;
    }

    [System.Runtime.InteropServices.DllImport("AresHash.dll", EntryPoint="Decode", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
    private static extern bool Decode(string hash, ref hash_channel hc);

    private void Test()
    {
      hash_channel hc = new hash_channel();
      bool ret = false;
      hc.VB = true;
      ret = Decode(txtHash, ref hc);
      if (ret == false)
      {
       MessageBox.Show("Error, not a valid hashlink");
       return;
      }

      txtExtIP.Text = hc.externalIP;
      txtIntIP.Text = hc.internalIP;
      txtPort.Text = hc.port;
      txtName.Text = hc.roomName;
      txtTopic.Text = hc.topic;

    }

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB.NET to C# Converter
    Instant VB: C# to VB.NET Converter
    Instant J#: VB.NET to J# Converter
    Clear VB: Cleans up outdated VB.NET code

    Friday, September 30, 2005 11:27 PM

All replies

  • The problem is that a VB6 "Long" is a VB.NET "Integer" and a VB6 "Integer" is a VB.NET "Short".

    Here is the translation (produced with our Instant C# VB to C# converter) after upgrading the VB6 code to VB.NET first:

    private struct hash_channel
    {
     public int externalIP;
     public int internalIP;
     public short port;
     public string roomName;
     public string topic;
     public bool VB;
    }

    [System.Runtime.InteropServices.DllImport("AresHash.dll", EntryPoint="Decode", ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
    private static extern bool Decode(string hash, ref hash_channel hc);

    private void Test()
    {
      hash_channel hc = new hash_channel();
      bool ret = false;
      hc.VB = true;
      ret = Decode(txtHash, ref hc);
      if (ret == false)
      {
       MessageBox.Show("Error, not a valid hashlink");
       return;
      }

      txtExtIP.Text = hc.externalIP;
      txtIntIP.Text = hc.internalIP;
      txtPort.Text = hc.port;
      txtName.Text = hc.roomName;
      txtTopic.Text = hc.topic;

    }

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB.NET to C# Converter
    Instant VB: C# to VB.NET Converter
    Instant J#: VB.NET to J# Converter
    Clear VB: Cleans up outdated VB.NET code

    Friday, September 30, 2005 11:27 PM
  • oH! ye, how dumb could i be to forget that. I changed the data types and it worked, thanks a lot
    Saturday, October 01, 2005 6:13 AM