Results 1 to 10 of 10

Thread: [2.0] DLL Import

  1. #1

    Thread Starter
    Hyperactive Member Rattlerr's Avatar
    Join Date
    Jul 2005
    Location
    FloralCity,Florida
    Posts
    269

    Question [2.0] DLL Import

    I need to import a DLL using the Class.cs...this is what i have but its not correct..

    VB Code:
    1. static [System.Runtime.InteropServices.DllImport("dll file name")]

    The Other issue i'm having is i used the Covert Tool in Visual Studio to upgrade a VB6 project to VB.NET,the Problem i having is with the F = FreeFile,
    F = Split(LTD, ":")....

    Here is the VB6 Code:

    VB Code:
    1. Dim LTD As String, F As Variant, x As Integer
    2.  
    3. F = FreeFile
    4.     With CommonDialog1
    5.         .FileName = ""
    6.         .DialogTitle = "Load bots List"
    7.         .Filter = "All Supported Types|*.txt"
    8.         .ShowOpen
    9.         If .FileName = "" Then Exit Sub
    10.         Open .FileName For Input As #F
    11.             While Not EOF(1)
    12.             Input #1, LTD
    13.             F = Split(LTD, ":")
    14.             If x < 1000 Then
    15.             Set Item = ListView1.ListItems.Add(, , , , 2)
    16.             Item.SubItems(1) = F(0)
    17.             Item.SubItems(2) = F(1)
    Now when i used the Convert tool in VS 2005 to VB.NET the F = FreeFile was changed too:

    F = FileSystem.FreeFile

    I know the FileSystem is refering to the VB FileSystem used in Visual Basic .NET..I need to get that Over into C# along with the F = Split(LTD, ":"),
    everything else i have already Reworked into C#..

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] DLL Import

    You should rewrite that section from scratch using the System.IO namespace. If you're reading text then use a StreamReader. The .NET 2.0 IO.File class also has methods like ReadAllLines. This allows you to create a string array of all the lines in a text file in one line of code. It uses a StreamReader under the covers. Once you have the array you can then iterate over it and split each element using String.Split and create your ListViewItems from that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member Rattlerr's Avatar
    Join Date
    Jul 2005
    Location
    FloralCity,Florida
    Posts
    269

    Re: [2.0] DLL Import

    Ok,so the openFileDialog isnt a good idea than ehh??
    VB Code:
    1. StreamReader sr = File.OpenText("Client.txt");
    2. OpenFileDialog openFileDialog1 = new OpenFileDialog();
    3.             openFileDialog1.InitialDirectory = "c:\\";
    4.             openFileDialog1.Filter = "txt files (*.txt)|*.txt|All Files (*.*) | *.*";
    5.             openFileDialog1.FilterIndex = 2;
    6.             openFileDialog1.RestoreDirectory = true;
    7.             openFileDialog1.Title = "Load Client List";
    8.             openFileDialog1.FileName = "";
    9.             while ((input = sr.ReadLine()) != null)
    10.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
    11.             {
    12.                 try
    13.                 {
    14.                     if ((myStream = openFileDialog1.OpenFile()) != null)
    15.                     {
    16.                         using (sr.ReadLine())
    17.                         {
    18.                             //insert code to read the stream here
    19.                         }
    20.                     }
    21.                 }
    22.                 catch (Exception ex)
    23.                 {
    24.                     MessageBox.Show("Error: Could Not Read File error: " + ex.Message);
    25.                 }
    26.             }
    Last edited by Rattlerr; Jul 24th, 2006 at 06:44 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] DLL Import

    If you want the user to select a file then the OpenFileDialog is required, but you can choose to read the file line by line with your own StreamReader or let the File class do it for you, then loop through the array it returns. You've got some problems with that last code though. It should be something more like:
    Code:
    using (OpenFileDialog ofd = new OpenFileDialog())
    {
        // Set properties of 'ofd' here.
    
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            using (StreamReader sr = new StreamReader(ofd.FileName))
            {
                while (sr.Peek() != -1)
                {
                    // Read a line of text and split it, then use it to create a ListViewItem.
                    this.listView1.Items.Add(new ListViewItem(sr.ReadLine().Split(':')));
                }
    
                sr.Close();
            }
        }
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member Rattlerr's Avatar
    Join Date
    Jul 2005
    Location
    FloralCity,Florida
    Posts
    269

    Re: [2.0] DLL Import

    So this line of code here:
    VB Code:
    1. this.listView1.Items.Add(new ListViewItem(sr.ReadLine().Split(':')));

    Replicates the F = Split(LTD, ":") ?????

    In the Text file for example the Username and Password are suppose to be like this:

    username : password if its not like that the program returns an Error...And only the Username show up in the listview1 with the Small Icon for Online & Offline...



    The other Question i have is the F = FreeFile...When i upgraded it to the VB.NET it came out like F = FileSystem.FreeFile,Which in C# FileSystem is used its Directory...So how would i replicate that into C#???

    VB Code:
    1. Directory FileSystem = new Directory(); // ????
    2. or
    3. struct Directory
    4. {
    5. F,
    6. FileSystem,
    7. FreeFile,
    8. }
    This is where F is used again = F(0) the 0 & 1 is a SmallIcon
    VB Code:
    1. Item.SubItems(1) = F(0) // VB6
    2.             Item.SubItems(2) = F(1)

    I'm just not sure how to Replicate F = FreeFile or F = FileSystem.FreeFile,
    I know the FileSystem is equal to Directory in C#..
    Last edited by Rattlerr; Jul 24th, 2006 at 07:26 PM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] DLL Import

    I don't see what use that "F = FreeFile" is anyway because before it's used again F is assigned a different value. Forget the VB6 code altogether. Don't even think about "converting". Just think about what you're trying to achieve and how best to do that in C#. What do you need to happen that my code in post #4 doesn't do?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member Rattlerr's Avatar
    Join Date
    Jul 2005
    Location
    FloralCity,Florida
    Posts
    269

    Re: [2.0] DLL Import

    Your right i'm just confusing myself all together....The only problem i need to Resolve is:

    VB Code:
    1. Open .FileName For Input As #F
    2.  
    3. Item.SubItems(1) = F(0)
    4. Item.SubItems(2) = F(1)

    Error 2 The name 'F' does not exist in the current context

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] DLL Import

    You're still trying to convert VB6 code. What is the object that you want to achieve? Is it that you want to open a text file? If so then creating the StreamReader in my code does that. Is it creating a ListView Item? If so then my code does that also. You haven't said what you're trying to achieve that the code I supplied doesn't do. My code prompts the user to select a file. You'll have to add code to set up the OFD as you want it. It then opens the selected file as a text file. It then reads each line, splits them on the colon character and creates a ListViewItem where each substring is in its own column. What else is it that you want?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member Rattlerr's Avatar
    Join Date
    Jul 2005
    Location
    FloralCity,Florida
    Posts
    269

    Re: [2.0] DLL Import

    ok the SubItems(0) is a Online SmallIcon, (1) is a Offline SmallIcon that shows up in the listView1 next to the username...

    Item = ListView1.ListItems.Add(, , , , 2)
    Item.SubItems(1) = F(0) // F produces the Error Below
    Item.SubItems(2) = F(1)

    Error 2 The name 'F' does not exist in the current context

    Your Code is what i needed, this is a Different issue m8..

    So my question is what would i declare F as ??

    int F; ???
    public string F; ??

  10. #10

    Thread Starter
    Hyperactive Member Rattlerr's Avatar
    Join Date
    Jul 2005
    Location
    FloralCity,Florida
    Posts
    269

    Re: [2.0] DLL Import

    I generated a method Stub for F ,that dropped the Error for now..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width