0

I have the following code:

Public Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function CloseClipboard Lib "user32" () As Long

Sub test()
Dim wat As String

OpenClipboard (0&)
wat = GetClipboardData(CF_TEXT)
MsgBox wat
CloseClipboard

End Sub

This always returns 0 (NULL) in the msgbox. Could somone tell me what is wrong with this code?

closed as off-topic by Ramhound, grawity, harrymc, Twisty Impersonator, BillP3rd Jan 20 at 3:16

  • This question does not appear to be about computer software or computer hardware within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • I would presume the problem is your call to OpenClipboard, what boolean value does it return? Edit your question to include this vital information – Ramhound Jan 7 at 12:18
  • @Ramhound how would I know that (in the sense how do I verify that)? – Noir Jan 7 at 12:26
  • The same way you verified the other method was returning 0. Use a variable to capture the return value. – Ramhound Jan 7 at 12:40
  • @Ramhound doesn't seem to be the brightest of days for me. Indeed I can confirm that the Boolean is 1. – Noir Jan 7 at 12:43
  • @Ramhound I think the code does actually work, it just doesn't return what I would expect it to. If the clipboard is empty, it returns 0, if it is not empty it returns a seemingly random number of integers (e.g. "*" returns 791549112). I would have expected it does return the actual copied text. – Noir Jan 7 at 12:56
1

GetClipboardData will deliver a handle to the global memory block. You will have to lock this block and retrieve the String from there.

Based on windows-api/retrieve-information-from-the-clipboard a quick an dirty example based on your question:

Public Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function CloseClipboard Lib "user32" () As Long
Public Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Public Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Public Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096






Sub test()
Dim wat As String
Dim hClipMemory As Long
Dim lpClipMemory As Long
Dim Dummy As Long
If OpenClipboard(0&) <> 0 Then    
  hClipMemory = GetClipboardData(CF_TEXT)
  If Not IsNull(hClipMemory) Then
    lpClipMemory = GlobalLock(hClipMemory)
    If Not IsNull(lpClipMemory) Then
        wat = Space$(MAXSIZE)
        Dummy = lstrcpy(wat, lpClipMemory)
        GlobalUnlock (hClipMemory)
    End If
  End If      
  MsgBox wat
CloseClipboard
End If

End Sub
  • Thank you @bummi. I am very new to calling the user32 and kernel32 libraries, consequently it seems I fully failed to understand how this code should have been executed. I awarded you the solution as what you posted actually does do what I would have wanted it to do. And thank you for the link I will examine the full code and documentation to get a better understanding of what is actually going on. – Noir Jan 7 at 13:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.