Calling Win32 functions from DLL library

G

Guest

Hello everyone,

I have read and learedn all about calling Win32 functions from DLL under VBA
and VB
BUT! The functions does not work. There is no syntax errors or runtime
errorsh functions just return zero or empyt string for output paramters.
If i declare something like this:
Private Declare Function GetSystemMetrics Lib "alabalauser32.dll" (ByVal
nIndex As Long) As Long instead:
Private Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As
Long) As Long

There is no change in behaviour of the program.

I am using Access 2003 from MS Office proffessional 2003 and VBA Visual
Basic 6.3 build 9969. It seems that this functionality is not available in
those version of VBA.

Thank you in advance for replays
 
G

Graham R Seach

Emil,

Maybe you've just simplified your post, but the feeling I get is that
declaring the APIs is all you've done. Forgive me if I misunderstood, but
not only do you have to declare the APIs, you also have to call them.

What are you trying to do?

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
G

Guest

Hi of course I call the API put I just want to thigs be simple.
I can say that the example from MSDN does not work.

Here it is the source code:
Module1
Option Compare Database
Option Explicit

Private Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As
Long) As Long

Public Const SM_CXFRAME = 32
Public Const SM_CYFRAME = 33

Public Property Get ScreenHeight() As Long
' Return screen height in pixels.

ScreenHeight = GetSystemMetrics(SM_CYFRAME)
End Property

Public Property Get ScreenWidth() As Long
' Return screen width in pixels.

ScreenWidth = GetSystemMetrics(SM_CXFRAME)
End Property


Module2
Option Compare Database
Option Explicit

Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal Hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Public Function ActiveWindowCaption() As String
Dim strCaption As String
Dim lngLen As Long

' Create string filled with null characters.
strCaption = String$(255, vbNullChar)
' Return length of string.
lngLen = Len(strCaption)

' Call GetActiveWindow to return handle to active window,
' and pass handle to GetWindowText, along with string and its length.
If (GetWindowText(GetActiveWindow, strCaption, lngLen) > 0) Then
' Return value that Windows has written to string.
ActiveWindowCaption = strCaption
End If
End Function

Form1
Option Compare Database
Option Explicit

Private Sub Command0_Click()
txtHeigth.SetFocus
txtHeigth.Text = Module2.ActiveWindowCaption
End Sub

Private Sub Command1_Click()
txtWidth.SetFocus
txtWidth.Text = Module1.ScreenHeight
End Sub

I have tried some code wiht odbc32 what is I am actulay need list of DSNs
SQLDataSources and SQLAllocEnv

Regards
Emil
 
D

Douglas J. Steele

And I can say that I just took your code, and it worked fine for me (Access
97/Win XP Pro, SP1)

You sure that the Command0_Click and Command1_Click are being invoked? (It
can sometimes happen that the OnClick property for the command button
doesn't actually point to the code that's supposed to be run)

DSNs are stored in the Registry. You can actually use any code that reads
the registry to get at them.
 
G

Guest

Hi Douglas,

Yes I am sure because I can trace it.
As I mentioned earlier the functions are called but does not return correct
result only zero or empty string. And this is reason to thing that the
problem is not in the code but my Access. For registry yes I saw that idea
but again I need Win32 in VBA. Using of ODBC API make your code portable in
future because tomorrow Microsoft theoretically can change the place of DSNs
or to be different on different Windows versions so SQLDataSources is the
proper function for that no doubt about this.

Best Regards
Emil
 
D

Douglas J. Steele

Well, Microsoft is extremely unlikely to change the place of DSNs, because
they don't write all the ODBC drivers. They'd break all other vendors
products, and I can't see them doing that.

However, take my word that there are no problems making API calls from
Access. While I only tested using Access 97, that's because that's all I
have installed on this machine. I make extensive use of API calls with
Access 2003.
 
G

Guest

OK I have tried the same db2.mdb (that with VBA code) on other computer with
the same windows and office installation and SP2 for Office 2003 and yes it
works! The only think I have more is the Visual Studio .Net and .Net 1.1. Can
that be the problem so what can be the reason invocations of DLL functions to
not work? How to fix that on my computer ?

Best Regards,
Emil
 
D

Douglas J. Steele

I'm unaware of .Net interfering with VBA. The machine on which I tested with
Access 97 has both Visual Studio.Net and the .Net 1.1 Framework.
 
G

Guest

Hello guys,

I found the problem. It was not logical but I uninstall .NET framework and
Visual Studio .NET and MS SQL Server but this does not fix the problem. It
is may be logical but problem is in Kaspersky anti-virus real time protection
when I turn it off the code has run. I suppose that the real time protection
disable calls to DLLS from macro languages such like VBA but this is very
dummy solution.

TO Attention of Microsoft please take in mind this problem because I have
learned for the same problem with Symantec Anti-virus programs.

Best regards to all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top