.NET Compact and call to native API from VB

M

Mikhail Gryaznov

Hi,
I am trying to get a call to native API function from inside VB code:


Public Class Form1
Public Declare Function GetDeviceCaps Lib "gdi32.dll" Alias
"GetDeviceCaps" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Public Declare Function GetDC Lib "user32.dll" (ByVal hwnd As Long) As
Long
Public Declare Function ReleaseDC Lib "user32.dll" (ByVal hwnd As Long,
ByVal hdc As Long) As Long
Public Declare Function GetDesktopWindow Lib "user32.dll" () As Long

Public Const LOGPIXELSX As Long = 88 'Logical pixels/inch in X
Public Const LOGPIXELSY As Long = 90 'Logical pixels/inch in Y

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim hwndS As Long, dcS As Long

'Get the windows handle for the desktop (screen)
hwndS = GetDesktopWindow()

'Get a device context for the above
dcS = GetDC(hwndS)

MsgBox("Horizontal Resolution: " & GetDeviceCaps(dcS, LOGPIXELSX))

'Release the device context to avoid GDI memory leaks
ReleaseDC(hwndS, dcS)
End Sub

End Class

When I run application on desktop in emulator mode I got and exception:
"Can't find PInvoke DLL 'user32.dll'"
at the line of calling to GetDesktopWindow().

Mikhail Gryaznov
 
C

Chris Tacke, eMVP

At first glance I see 3 problems:

1. Long is 64-bit, so that's certainly not right. Should be Integer.
2. gdi32.dll doesn't exist in CE. Use coredll.dll
3. user32.dll doesn't exist in CE. Use coredll.dll

I'm certain these are defined and wrapped many places on the web. Why are
you reinventing the wheel?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 

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