Calling Windows API functions with string parameters from Access 
Author Message
 Calling Windows API functions with string parameters from Access

How do you call a Windows API function which has a string (i.e. pointer to
character) as a parameter? The following code gives an invalid page fault.
Any ideas?

Thanks,

Neil McCrossin

Option Compare Database
Option Explicit

Private Declare Function GetComputerName Lib "kernel32.dll" Alias
"GetComputerNameA" (lpBuffer As String, nSize As Long) As Boolean

Private Sub Command0_Click()
    Dim ComputerName As String, ComputerNameLen As Long

    ComputerName = String(50, 0)
    ComputerNameLen = Len(ComputerName)
    Call GetComputerName(ComputerName, ComputerNameLen)
    MsgBox (ComputerName)
End Sub



Tue, 17 Apr 2001 03:00:00 GMT  
 Calling Windows API functions with string parameters from Access

Quote:

>How do you call a Windows API function which has a string (i.e. pointer to
>character) as a parameter? The following code gives an invalid page fault.
>Any ideas?

>Thanks,

>Neil McCrossin

>Option Compare Database
>Option Explicit

>Private Declare Function GetComputerName Lib "kernel32.dll" Alias
>"GetComputerNameA" (lpBuffer As String, nSize As Long) As Boolean

>Private Sub Command0_Click()
>    Dim ComputerName As String, ComputerNameLen As Long

>    ComputerName = String(50, 0)
>    ComputerNameLen = Len(ComputerName)
>    Call GetComputerName(ComputerName, ComputerNameLen)
>    MsgBox (ComputerName)
>End Sub

You need to pass strings to APIs using ByVal - this converts VB strings to
C-style strings.

Simon Lewis



Tue, 17 Apr 2001 03:00:00 GMT  
 Calling Windows API functions with string parameters from Access
Private Declare Function GetComputerName Lib "kernel32" Alias
"GetComputerNameA" (byVal lpBuffer As String, nSize As Long) As Long

In general you declare strings with ByVal, also note the return value is a
long not a boolean.

If you are going to make much use if APIs I suggest you get hold of a good
reference book such as Dan Appleman's Visual Basic Programmers Guide to the
Win32 API

Quote:

>How do you call a Windows API function which has a string (i.e. pointer to
>character) as a parameter? The following code gives an invalid page fault.
>Any ideas?

>Thanks,

>Neil McCrossin

>Option Compare Database
>Option Explicit

>Private Declare Function GetComputerName Lib "kernel32.dll" Alias
>"GetComputerNameA" (lpBuffer As String, nSize As Long) As Boolean

>Private Sub Command0_Click()
>    Dim ComputerName As String, ComputerNameLen As Long

>    ComputerName = String(50, 0)
>    ComputerNameLen = Len(ComputerName)
>    Call GetComputerName(ComputerName, ComputerNameLen)
>    MsgBox (ComputerName)
>End Sub



Tue, 17 Apr 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Function call via string parameter in other function ?

2. String parameter API calls in 32 bit

3. String problems calling a C API functions in VB5

4. calling windows api functions on server

5. How to Call Windows API functions in VBScript?

6. Help on Windows API function calls

7. Access 2 and Windows 98 - Illegal Function Call

8. API Call for Access 2 , Windows 3.11 Wanted

9. calling access querydef that calls a user defined access function

10. Windows API call to set windows colors (HELP Please)

11. API Calls in Windows NT as well as Windows 95/98

12. Using string versus variant as function parameters

 

 
Powered by phpBB® Forum Software