Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clipboard()->AsText access violation, checking using timer

Status
Not open for further replies.

Chris8852

Programmer
Jan 15, 2003
16
0
0
GB
In my program I use code like

AnsiString b1 = Clipboard()->AsText;
and
int i = Clipboard()->AsText.Length();

Both cause access violations. My program uses AsText hundreds of times in it, but not all the time does it cause an access violation. I check to see if the Clipboard() == NULL and if Clipboard()->AsText == NULL.

Clipboard()->AsText == NULL causes an access violation

if(Clipboard() == NULL) returns false

So, as a workaround to using a timer to check for clipboard changes, I tried using the OnMessag event in TApplicationEvents...

void __fastcall TForm1::FormCreate(TObject *Sender)
{
NextInChain = SetClipboardViewer(Form1->Handle);
}


void __fastcall TForm1::ApplicationEvents1Message(tagMSG &Msg,
bool &Handled)
{
if (Msg.message == WM_DRAWCLIPBOARD)
{
//ShowMessage("1");
Beep();
}


if(Msg.message == WM_CHANGECBCHAIN)
{
Beep();
HWND Remove, Next;

Remove = (HWND)Msg.wParam;
Next = (HWND)Msg.lParam;


if(NextInChain == Remove)
NextInChain = Next;
else if (NextInChain != 0)
SendMessage(NextInChain, WM_CHANGECBCHAIN,
(long)Remove, (long)Next);

}

}

I don't seem to get either of those two messages at any point in the program, even when I copy to the clipboard!

I also tried;

void __fastcall TForm1::FormCreate(TObject *Sender)
{
Application->OnMessage = AppMessage;
}


void __fastcall TForm1::AppMessage(tagMSG &Msg, bool &Handled)
{
if (Msg.message == WM_DRAWCLIPBOARD)
{
//ShowMessage("1");
Beep();
}


if(Msg.message == WM_CHANGECBCHAIN)
{
Beep();
HWND Remove, Next;

Remove = (HWND)Msg.wParam;
Next = (HWND)Msg.lParam;


if(NextInChain == Remove)
NextInChain = Next;
else if (NextInChain != 0)
SendMessage(NextInChain, WM_CHANGECBCHAIN,
(long)Remove, (long)Next);

}

}

That doesn't work either. :(
 
Straight from the help files.
TClipboard::AsText

Represents the content of the clipboard as a string.

__property System::AnsiString AsText = {read=GetAsText, write=SetAsText};

Description

Use the AsText property to place text in and retrieve text from the clipboard. The clipboard must contain a string or an exception occurs. To determine if the clipboard contains a string, pass CF_TEXT to the HasFormat method.

//////end of help

Seems you should check the format before trying to access any text. If the clipboard contains an image you wont get
a string and you will throw an exception.
 
Yes I read that and I DO check to make sure the clipboard contains text before I use it.
This isn't the problem. I test it by copying text to the clipboard, and that is when I get the access violations.
Is there a way I can have the access violation just not show up?...
 
maybe your test is incorrect.
try testing the format parameters first.

check the clipboard for format
format changed to something other than text
do something
format not changed
use gettextbuff to copy the clipboard to a posibly
quite large char string [].
get string length
if length has changed from previous
do something.
if all Im doing is to check for changes that is.

TClipboard::HasFormat
Indicates whether the clipboard contains data in a specified format.

bool __fastcall HasFormat(Word Format);

Description

Use HasFormat to find out whether the clipboard contains data encoded in a specific format. HasFormat returns true if the format is present and false otherwise. TClipboard keeps a list of available formats in the Formats array property.

Some of the possible values of the Format parameter follow. HasFormat supports additional formats provided by Windows or by other applications as well as custom formats registered with TPicture::RegisterClipboardFormat.

Value Meaning

CF_TEXT - Text with a CR-LF combination at the end of
each line. A null character identifies the end
of the text.
CF_BITMAP - A Windows bitmap graphic.
CF_METAFILEPICT - A Windows metafile graphic.
CF_PICTURE - An object of type TPicture.
CF_COMPONENT - Any persistent object.
 
Yes, I already am checking it with CF_TEXT.
Does anyone here know how to get the clipboard notification code working?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top