October 30, 2025, 02:09:18 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


global alloc

Started by kryton9, September 05, 2006, 09:52:33 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

kryton9

I am trying to use global alloc. I want to be able to put multiple lines of text to the clipboard.

With this setting I can do 2 lines of text and 2 border lines. For a total of 4 lines.
hdlMemory = GlobalAlloc(GHND,255);

No matter what I have tried to do to increase to allocate more memory it never seems to increase.

Here is the last thing I tried after a zillion combinations of flags and trying to allocate memory, and many different sizeof(types) and just placing GlobalAlloc(GHND,255*x) x being many different numbers.
hdlMemory = GlobalAlloc(0x02,sizeof(dstring));

Here is the MSDN link for Global Alloc
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/globalalloc.asp

I want to be able to set aside for flexible memory as I don't know how many lines the user will type into the richedit box that I need to copy to the clipboard.
Thanks in advance.

Ionic Wind Support Team

You need to lock the memory before writing to it.  Make sure you have the constants for GHND set correctly.
Ionic Wind Support Team

Ionic Wind Support Team

here is a quickie example.


DECLARE IMPORT,OpenClipboard(UINT hwnd),INT;
DECLARE IMPORT,CloseClipboard(),INT;
DECLARE IMPORT,GetClipboardData(int format),INT;
DECLARE IMPORT,SetClipboardData(int format,int handle),INT;
DECLARE IMPORT,EmptyClipboard(),INT;
DECLARE IMPORT,IsClipboardFormatAvailable(int format),INT;
DECLARE IMPORT,GlobalAlloc(int flags,int bytes),UINT;
DECLARE IMPORT,GlobalLock(UINT handle),POINTER;
DECLARE IMPORT,GlobalUnlock(UINT handle),INT;
const CFTEXT = 1;
const GHND = 0x42;


class clipwin:CWindow
{
declare virtual OnClose(),int;
declare virtual OnKeyDown(unsigned int nChar, INT nRepCnt, unsigned int nFlags),int;
}

global sub main()
{
clipwin win;
win.Create(0,0,640,480,AWS_VISIBLE|AWS_AUTODRAW|AWS_SIZE|AWS_SYSMENU,0,"Clipboard Example",NULL);

dstring text[1024];
text = "This text was copied to the clipboard, This text was copied to the clipboard, This text was copied to the clipboard, This text was copied to the clipboard, This text was copied to the clipboard, This text was copied to the clipboard, This text was copied to the clipboard, This text was copied to the clipboard";
// Allocate a globle chunk of memory as a 'handle'
UINT handle = GlobalAlloc(GHND,1024);
// lock the memory so we can write to it
string *lock = GlobalLock(handle);
// Write the contents of the string to the memory block
*lock = text;
// Unlock the memory so we can send it to the clipboard
GlobalUnlock(handle);
// Open and empty the clipboard
OpenClipboard(win.GetHandle());
EmptyClipboard();
// Place the memory on the clipboard
SetClipboardData(CFTEXT,handle);
// and finally close the clipboard
CloseClipboard();
// There is no need to 'Free' the memory allocated with GlobalAlloc
// since once its placed on the clipboard the system 'owns' it.
win.WriteText(0,10,"Press CTRL-V to paste text");
do
{
wait();
} until !win.IsValid();
}

clipwin::OnClose(),int
{
Destroy();
}

clipwin::OnKeyDown(unsigned int nChar, INT nRepCnt, unsigned int nFlags),int
{
dstring text[1024];
IF GETKEYSTATE(0x11)
{
// is the key the 'v' key? If so lets
// do a paste from the clipboard
IF ((nChar = 'V') || (nChar = 'v'))
{
// check to make sure the TEXT format is there
IF IsClipboardFormatAvailable(CFTEXT)
{
// open the clipboard and get a lock on the memory
OpenClipboard(GetHandle());
UINT handle = GetClipboardData(CFTEXT);
string *lock = GlobalLock(handle);
// copy the contents of the memory to the string
text = *lock;
GlobalUnlock(handle);
CloseClipboard();
// a simple print of the contents
WriteText(0,30,text);
//Print the length of the text;
WriteText(0,50,"Length  = " + NumtoStr(len(text),0));
}
}
}

}
Ionic Wind Support Team

kryton9

Thanks Paul, looking at your example helped. I guess I was doing Global Alloc correctly, it was that I didn't set the string or in this case dstring correctly. It was the limiting factor, as I had it set to string, so no matter what I did the 255, it just pointed to a string's worth. Live and learn.

Thanks again, your nice example helped me see the mistake right away!!