www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Programming with windows api (windows.h)

reply "monarch_dodra" <monarchdodra gmail.com> writes:
I'm a bit confused about how to interface with windows.h.

All I'm trying to do is retrieve some text from my clipboard to 
print it.

So far, I have this:

//----
import std.stdio;

import core.sys.windows.windows;

extern (Windows)
{
   HWND   GetClipboardOwner();
   BOOL   OpenClipboard(HWND hWndNewOwner);
   HANDLE GetClipboardData(UINT uFormat);
}

void main()
{
   HWND hWnd = GetClipboardOwner();

   if ( hWnd == null ) {stderr.writeln("Problem."); return;}
   if ( OpenClipboard(hWnd) == 0 ) {stderr.writeln("Problem 2."); 
return;}

   hWnd = cast(HWND)GetClipboardData(CF_TEXT);
}
//----

This works, but as you can see, I have to declare as "extern 
(Windows)" all the functions myself. Not pretty, but works I 
guess. I'm hitting a wall at "CF_TEXT" though: That's an enum.

Anyways...

Am I doing it wrong, or are is the amount of ported windows 
interface currently limited...

Kinda lost here (The fact that I'm also new to windows 
programming doesn't help...) :)
Dec 17 2012
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 12/17/12, monarch_dodra <monarchdodra gmail.com> wrote:
 Am I doing it wrong, or are is the amount of ported windows
 interface currently limited...
std.c.windows.windows is a very thin layer around the API. There are better bindings here: http://dsource.org/projects/bindings/wiki/WindowsApi
 Kinda lost here (The fact that I'm also new to windows
 programming doesn't help...) :)
Take a look here, there are many examples of WinAPI programming: https://github.com/AndrejMitrovic/DWinProgramming
Dec 17 2012
parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Monday, 17 December 2012 at 17:24:40 UTC, Andrej Mitrovic 
wrote:
 On 12/17/12, monarch_dodra <monarchdodra gmail.com> wrote:
 Am I doing it wrong, or are is the amount of ported windows
 interface currently limited...
std.c.windows.windows is a very thin layer around the API. There are better bindings here: http://dsource.org/projects/bindings/wiki/WindowsApi
 Kinda lost here (The fact that I'm also new to windows
 programming doesn't help...) :)
Take a look here, there are many examples of WinAPI programming: https://github.com/AndrejMitrovic/DWinProgramming
Good deal. I was able to get it to work with that. Thanks.
Dec 20 2012
prev sibling parent reply "John Chapman" <johnch_atms hotmail.com> writes:
On Monday, 17 December 2012 at 16:21:09 UTC, monarch_dodra wrote:
 I'm a bit confused about how to interface with windows.h.

 All I'm trying to do is retrieve some text from my clipboard to 
 print it.
Something like this: HWND hwnd = ... // your window handle (or null) if (OpenClipboard(hwnd)) { HGLOBAL data = GetClipboardData(CF_TEXT); if (data) { auto text = cast(wchar*)GlobalLock(data); writeln(to!string(text[0 .. wcslen(text)])); GlobalUnlock(data); } } You'll have to define GlobalLock/GlobalUnlock, and values for the various clipboard formats (CF_TEXT and co) are documented here: http://msdn.microsoft.com/en-gb/library/windows/desktop/ff729168%28v=vs.85%29.aspx
 I'm hitting a wall at "CF_TEXT" though: That's an enum.
enum : uint { CF_TEXT = 1, CF_BITMAP = 2, // and so on }
Dec 17 2012
parent "John Chapman" <johnch_atms hotmail.com> writes:
   HGLOBAL data = GetClipboardData(CF_TEXT);
Oops, should be CF_UNICODETEXT (13)
Dec 17 2012