Results 1 to 9 of 9

Thread: What's wrong with this code? (semi-resolved)

  1. #1

    Thread Starter
    Addicted Member ender_pete's Avatar
    Join Date
    Aug 2000
    Location
    Virginia, United States
    Posts
    131
    in vb.net to get a value back from a function you dont set the return value to the function name.

    You do the Return statement. Like this.

    Code:
    Option Strict On
    Module modGetInfo
    
    	Public Declare Function GetVolumeInformation Lib "kernel32" _
    	 Alias "GetVolumeInformationA" _
    	 (ByVal lpRootPathName As String, _
    	 ByVal lpVolumeNameBuffer As String, _
    	 ByVal nVolumeNameSize As Long, _
    	 ByVal lpVolumeSerialNumber As Long, _
    	 ByVal lpMaximumComponentLength As Long, _
    	 ByVal lpFileSystemFlags As Long, _
    	 ByVal lpFileSystemNameBuffer As String, _
    	 ByVal nFileSystemNameSize As Long) As Long
    
    	Function GetHardDriveSerialNumber() As Long
    
    		Dim Result As Long
    		Dim RootPathName As String = "C:"
    		Dim VolumeNameBuffer As String = Space(256)
    		Dim VolumeNameSize As Long = 256
    		Dim VolumeSerialNumber As Long = 0
    		Dim MaximumComponentLength As Long = 256
    		Dim FileSystemFlags As Long = 0
    		Dim FileSystemNameBuffer As String = Space(256)
    		Dim FileSystemNameSize As Long = 256
    
    		Result = GetVolumeInformation(RootPathName, _
    			VolumeNameBuffer, _
    			VolumeNameSize, _
    			VolumeSerialNumber, _
    			MaximumComponentLength, _
    			FileSystemFlags, _
    			FileSystemNameBuffer, _
    			FileSystemNameSize)
    
                      'do this 
    		Return VolumeSerialNumber
                      'instead of this
                      GetHardDriveSerialNumber = VolumeSerialNumber
    
     
    	End Function
    
    
    End Module
    that should work for you
    ender_pete
    C#,VS.NET Ent Arch, vb6 ee sp5,html,vbscript,jscript,
    xml,dhtml,delphi,c++,vc++,java,cgi,php, python, ada(so ancient) ,adasage(also ancient) and others i can't remember.....

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Posted by ender_pete
    in vb.net to get a value back from a function you dont set the return value to the function name.
    Actually you can set the return value to the function name.

  3. #3
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Unhappy

    The problem is still unsolved, but thanks at least for your interest and your attempts.

    Regarding the method of return, you are both right - you can do it either way, but I don't think that is the issue here.

    I have a suspicion it is something to do with the string declares.

    In VB6 I would say

    VB Code:
    1. Dim VolumeNameBuffer As String * 256

    which is a fixed-length string

    VB Code:
    1. Dim VolumeNameBuffer As String = Space(256)

    is NOT a fixed length string, I think. But how else can I declare a fixed length string in .net?

    I know about the nasty

    VB Code:
    1. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=100> Public s As String

    (have not tried it yet), but isn't that just an Upgrade Wizard kludge? Isn't there a native way to declare fixed length strings in .net?

    Anyway, if the API call is falling flat on its face for some reason - whether passing the wrong type of arguments or not - then why does it return a non-zero (success) value?

    Me am think very strange (to quote I. R. Baboon.)
    Last edited by BrianHawley; Feb 17th, 2002 at 01:41 AM.
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  4. #4
    Lively Member cargobay69's Avatar
    Join Date
    Nov 2001
    Location
    Kessel Prison Camp
    Posts
    97
    I don't think that you use API functions in the same way as we did in VB 6. Instead of using function declarations, I believe that you have to use the Imports keyword to refer to the system object that contains the function or class that you want to use.
    VB Code:
    1. Imports System
    2. Imports System.Environment
    Not sure what the .net version of your specific function is though.
    Maybe check out The KPD-Team 2001 website. Their API-Guide that is available for download might provide some help.
    Darrin@CB69
    -----------------------------------------------
    Arrogance kills brain cells
    -----------------------------------------------
    Private Sub Sandwich (big As Byte)
    On Error GoTo Pub

  5. #5
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Smile

    Yes, I'm coming around to that way of thinking - although I think the approach is via system.management rather than system.environment. I understand that .net DOES support 'traditional' API calls, but that it is viewed as backward compatability and not encouraged. It's just that we have a LOT of code to port, and were looking for a method that required the least changes possible. That would of course be altering the API calls to work in .net, rather than rewriting them all - but it looks as if rewriting may be easier - particularly if failed call returns a 'success' token, which is going to play hell with the apps. Better it just failed.

    That KPD Team site is OUTSTANDING. Wish I had found it long ago.

    Thanks for the help.
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  6. #6
    Member otherones's Avatar
    Join Date
    Jun 2002
    Location
    Milk Factory
    Posts
    59
    This is how i have been "emulating" the fixed length string declare for dotnet

    VB Code:
    1. Dim str as string * 100
    2.  
    3. Dim str As New String(" ", 100)
    CSC

  7. #7
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    try setting the rootpath string to c:\ instead of just c:

    yeah that is porbably a long shot..but worth a try anyway.
    Hint: My long shot guesses always seem to be right on the money 90% of the time :
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  8. #8
    Sascha
    Guest
    When declaring API calls, you be carefull which params have to be set byval and which byref

    you need the backslash after the driveletter

    in VB.Net you should use Integers instead of Longs

    good luck

    Sascha

    VB Code:
    1. Public Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" _
    2.      (ByVal lpRootPathName As String, _
    3.       ByVal lpVolumeNameBuffer As String, _
    4.       ByVal nVolumeNameSize As Integer, _
    5.       ByRef lpVolumeSerialNumber As Integer, _
    6.       ByRef lpMaximumComponentLength As Integer, _
    7.       ByRef lpFileSystemFlags As Integer, _
    8.       ByVal lpFileSystemNameBuffer As String, _
    9.       ByVal nFileSystemNameSize As Integer) As Integer
    10.  
    11. Function GetHardDriveSerialNumber() As Integer
    12.     Dim Result As Integer
    13.     Dim RootPathName As String = "C:\"
    14.     Dim VolumeNameBuffer As String = Space(256)
    15.     Dim VolumeNameSize As Integer = 256
    16.     Dim VolumeSerialNumber As Integer = 0
    17.     Dim MaximumComponentLength As Integer = 256
    18.     Dim FileSystemFlags As Integer = 0
    19.     Dim FileSystemNameBuffer As String = Space(256)
    20.     Dim FileSystemNameSize As Integer = 256
    21.     Result = GetVolumeInformation(RootPathName, _
    22.                       VolumeNameBuffer, _
    23.                       VolumeNameSize, _
    24.                       VolumeSerialNumber, _
    25.                       MaximumComponentLength, _
    26.                       FileSystemFlags, _
    27.                       FileSystemNameBuffer, _
    28.                       FileSystemNameSize)
    29.  
    30.     Return VolumeSerialNumber
    31. End Function

  9. #9
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Please rate my post.

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