problems with copying data to clipboard

Hi, I've been working on this for some time, and I can't figure out my
problem.  I'm using samples straight from the newsgroups for my code,
but I get an Unhandled Exception: User breakpoint when my code calls
SetClipboardData.

Here is my code.  It gets text from a CListbox, coverts it to a
CString, and follows the usual procedure for pasting it to the
clipboard.  I've changed the strcpy line from copying the curItem
string to copying a simple "test" string, and it doesn't work either.

Please help!!!

Jay



Here is my code:

void CCRCCalculatorDlg::OnBnClickedCopyButton()
{
CString curItem;
int nIndex=m_ctl_CRCoutput.GetCurSel();
int n=m_ctl_CRCoutput.GetTextLen(nIndex);
m_ctl_CRCoutput.GetText(nIndex,curItem.GetBuffer(n));

HANDLE hMemory;
 hMemory=GlobalAlloc(GHND,curItem.GetLength()+1);
 {return;
 }
 char* pchData;
 pchData=(char *)GlobalLock(hMemory);
 strcpy(pchData,LPCSTR(curItem)); //I can change this to
strcpy(pchData,"test"); with no effect
 GlobalUnlock(hMemory);

if(OpenClipboard())
 {
  if(!EmptyClipboard())
   { return;
   }
  htemp=SetClipboardData(CF_TEXT,hMemory); //This is where the
exception occurs, taking me to the wincore.cpp function below
  CloseClipboard();
 }

}

/////This is the function where the unhandled exception is

AfxWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	// special message which identifies the window as using AfxWndProc
	if (nMsg == WM_QUERYAFXWNDPROC)
		return 1;

	// all other messages route through message map
	CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);
	ASSERT(pWnd != NULL);
	ASSERT(pWnd->m_hWnd == hWnd);
	if (pWnd == NULL || pWnd->m_hWnd != hWnd)
		return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
	return AfxCallWndProc(pWnd, hWnd, nMsg, wParam, lParam);
}

0
11/1/2005 2:38:19 PM
vc.mfc 33608 articles. 0 followers. Follow

3 Replies
390 Views

Similar Articles

[PageSpeed] 39

See below...
On 1 Nov 2005 06:38:19 -0800, jay_welbourn@hotmail.com wrote:

>Hi, I've been working on this for some time, and I can't figure out my
>problem.  I'm using samples straight from the newsgroups for my code,
>but I get an Unhandled Exception: User breakpoint when my code calls
>SetClipboardData.
>
>Here is my code.  It gets text from a CListbox, coverts it to a
>CString, and follows the usual procedure for pasting it to the
>clipboard.  I've changed the strcpy line from copying the curItem
>string to copying a simple "test" string, and it doesn't work either.
>
>Please help!!!
>
>Jay
>
>
>
>Here is my code:
>
>void CCRCCalculatorDlg::OnBnClickedCopyButton()
>{
>CString curItem;
>int nIndex=m_ctl_CRCoutput.GetCurSel();
>int n=m_ctl_CRCoutput.GetTextLen(nIndex);
>m_ctl_CRCoutput.GetText(nIndex,curItem.GetBuffer(n));
***
This is convoluted code to accomplish a simple task! Why not
	m_ctl_CRCoutput.GetText(nIndex, curItem);
which is a lot simpler?  The fact that you have failed to ReleaseBuffer is almost
certainly the cause, but since there was no need to do a GetBuffer, just get rid of the
code entirely!

****
>
>HANDLE hMemory;
> hMemory=GlobalAlloc(GHND,curItem.GetLength()+1);
***
hMemory = GlobalAlloc(GHND, (curtime.GetLength() + 1) * sizeof(TCHAR));
Program Unicode-aware, always!
***
> {return;
> }
> char* pchData;
> pchData=(char *)GlobalLock(hMemory);
****
Forget that char * exists!  Please!  The correct declaration is
LPTSTR pchData;
pchData = (LPTSTR)GlobalLock(hMemory);
****
> strcpy(pchData,LPCSTR(curItem)); //I can change this to
>strcpy(pchData,"test"); with no effect
****
_tcscpy(pchData, curItem); // still a bad idea, see later...
The fact that you put an LPCSTR cast there is already indicative of serious trouble.
Besides the fact that the CString remains invalid until ReleaseBuffer is done is bad
enough, but just forget that 8-bit characters are the natural mode, and always work in the
model of LPCTSTR if you need a cast (which you don't in this case, because a CString will
always cast correctly to an LPCTSTR)
****
> GlobalUnlock(hMemory);
>
>if(OpenClipboard())
> {
>  if(!EmptyClipboard())
>   { return;
>   }
>  htemp=SetClipboardData(CF_TEXT,hMemory); //This is where the
>exception occurs, taking me to the wincore.cpp function below
>  CloseClipboard();
> }
****
This is almost certainly due to your failure to ReleaseBuffer, because the buffer pointer
is essentially nonsense until ReleaseBuffer is done.  So you probably overwrote memory
which clobbered something else important.

When working with CStrings, always write the simplest code.  GetBuffer was not necessary,
and its presence has led to problems.

Also, it is worth pointing out that the error did not occur "in this function"...it
occured at some very specific line in this function, which the debugger should tell you,

Clean the code up and see if the problem persists.  It could be that you have other
problems and this is failing because of earlier damage.
****
>
>}
>
>/////This is the function where the unhandled exception is
>
>AfxWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
>{
>	// special message which identifies the window as using AfxWndProc
>	if (nMsg == WM_QUERYAFXWNDPROC)
>		return 1;
>
>	// all other messages route through message map
>	CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);
>	ASSERT(pWnd != NULL);
>	ASSERT(pWnd->m_hWnd == hWnd);
>	if (pWnd == NULL || pWnd->m_hWnd != hWnd)
>		return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
>	return AfxCallWndProc(pWnd, hWnd, nMsg, wParam, lParam);
>}
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15975)
11/1/2005 8:47:22 PM
Hi,


"jay_welbourn@hotmail.com" wrote:

> Hi, I've been working on this for some time, and I can't figure out my
> problem.  I'm using samples straight from the newsgroups for my code,
> but I get an Unhandled Exception: User breakpoint when my code calls
> SetClipboardData.
> 
> Here is my code.  It gets text from a CListbox, coverts it to a
> CString, and follows the usual procedure for pasting it to the
> clipboard.  I've changed the strcpy line from copying the curItem
> string to copying a simple "test" string, and it doesn't work either.
> 
> Please help!!!
> 
> Jay
> 
> 
> 
> Here is my code:
> 
> void CCRCCalculatorDlg::OnBnClickedCopyButton()
> {
> CString curItem;
> int nIndex=m_ctl_CRCoutput.GetCurSel();
> int n=m_ctl_CRCoutput.GetTextLen(nIndex);
> m_ctl_CRCoutput.GetText(nIndex,curItem.GetBuffer(n));

Your problem is here. Do not use GetBuffer it causes
inproper handling of the string buffer later. Instaed, 
an easer and accurate way is:
m_ctl_CRCoutput.GetText(nIndex, curItem);


> HANDLE hMemory;
>  hMemory=GlobalAlloc(GHND,curItem.GetLength()+1);
>  {return;
>  }
>  char* pchData;
>  pchData=(char *)GlobalLock(hMemory);
>  strcpy(pchData,LPCSTR(curItem)); //I can change this to
> strcpy(pchData,"test"); with no effect
>  GlobalUnlock(hMemory);
> 
> if(OpenClipboard())
>  {
>   if(!EmptyClipboard())
>    { return;
>    }
>   htemp=SetClipboardData(CF_TEXT,hMemory); //This is where the
> exception occurs, taking me to the wincore.cpp function below
>   CloseClipboard();
>  }
> 
> }
> 
> /////This is the function where the unhandled exception is
> 
> AfxWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
> {
> 	// special message which identifies the window as using AfxWndProc
> 	if (nMsg == WM_QUERYAFXWNDPROC)
> 		return 1;
> 
> 	// all other messages route through message map
> 	CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);
> 	ASSERT(pWnd != NULL);
> 	ASSERT(pWnd->m_hWnd == hWnd);
> 	if (pWnd == NULL || pWnd->m_hWnd != hWnd)
> 		return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
> 	return AfxCallWndProc(pWnd, hWnd, nMsg, wParam, lParam);
> }
> 



-- 
======
Arman

0
armancho_x1 (249)
11/1/2005 9:30:06 PM
Thanks Joseph and Arman, you've solved my problem and I'm totally
grateful!

I should really invest in a book and try to learn this stuff properly
so that I'm not always piecing together different bits of (not always
compatible) code.  

Cheers!
Jay

0
11/2/2005 5:30:37 PM
Reply:

Similar Artilces:

data from a modeless dialog box to its parent
I need to post a text data from a modeless dialog box to its parent which is itself a dialog based application. Since it should be a large text data a simple Windows message won't be good. What is the right way to do that? Thanks Gil If you are sure the data will stay current you can post the address of the data (as a pointer) in a windows message to the parent. If you want to be really sure use SendMessage() which will wait until the parent gets the message before returning. You could call a function or copy the data to a variable in the parent dialog, but that could be dang...

highlight range, apply calculation to data in cells and paste special to same range
I know how to select a range of cells and copy: Range("O2").Select Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select Selection.Copy but now 1. applying the calculation, which should be value of cell * 1000 2. special past values only to same selected sells ? You enter 1000 in a separate blank cell and copy it then highlight the range to be multiplied and past special-> multiply. that will multiply all the cells by 1000 in the pasted range. Regards, OssieMac "S Himmelrich" wrote: > I know how to select a range of cells and copy: &g...

problem with multiple SMTP domains in Exchange Server 2003
Hi there I have added a second SMTP domain to the Email Addresses (Policy) as follows: Recipients > Recipient Policies > Default Policy > Email Addresses (Policy) > New ... > SMTP Address > @xyz.com > (checked) This Exchange Organization is responsible for all mail delivery > to this address > Apply. When I do so, I no longer receive external emails on the first SMTP domain (@abc.com). Is this related to relay settings, or perhaps something else? Best regards Loane ...

CRM 4.0: WebSite deployment problem
CRM 4.0: I cannot deploy my WebSite in the CRM WebSite, below is the error: 'Microsoft.Crm.WebServices.Crm2007.MultipleOrganizationSoapHeaderAuthentica­tionProvider, Microsoft.Crm.WebServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' doesn't exist. Parameter name: Microsoft.Crm.WebServices.Crm2007.MultipleOrganizationSoapHeaderAuthenticat­ionProvider, Microsoft.Crm.WebServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Description: An unhandled exception occurred during the execution of the current web request....

pulling data 04-27-10
I'm not sure I am explaining this right I want the data from C21 on sheet 1 in a cell on sheet 2 if the A & B column data matches SHEET 1 row 21 A B C D E 4/21/2010 34287 74 3 4 SHEET 2 row 20 A B C D E 4/21/2010 34287 74 try =sumproduct((s1!a2:a22=a2)*(s1!b2:b22=b2)*s1!c2:c22) -- Don Guillett Microsoft MVP Excel SalesAid Software dguillett@gmail.com "Norm" <Norm@discussions.microsoft.com> wrote in message news:A40C8E08-721F-47BB-A73C-458AC...

MS CRM 4.0 Rollup 2 error Data ProductInfo.ExistingLanguage is not available
Recently upgraded to CRM 4.0. Details are as follows - CRM is on premise - 60 Outlook clients - 32 bit server w/ SRS and Exchange router on CRM server - 64 bit DB CRM 4.0 upgrade goes well. Server application of Rollup 2 works fine. Most of my 60 Outlook clients are getting a popup upon running the rollup 2 .EXE: "Data ProductInfo.ExistingLanguage is not available" These are all US English installs (language id 1033). This is not a multi-language install. All users were made "local admins" for the purpose of the client upgrade to 4.0 as well as the rollup install. About...

Save a Copy/Overwrite changes dialog box
All of a sudden, this dialog box pops up every time I want to save something in Excel 2002. The box says, "The file '<file name>' may have been changed by another user since you last saved it. In that case, what do you want to do? __ Save a Copy __ Overwrite changes even though I'm the only user that's changed anything in the past 3 years. The shared workbook command has not been changed at all, so that can't be the problem. The settings are the same as the ones I have at home for personal spreadsheets that don't have this dialog box. Help! Than...

Copy and Paste.
Hello Folks, Hope you are all well. What I have tried to do is select every object on a page and paste it into a new publication. Sounds simple? However, when I try to do this on a page which has a table in it and then try to paste that, the size of the table goes haywire. It then becomes a manual task again to have to resize it / put text back to where it belongs etc. Please can someone tell me why this is happening and if there is a way to fix to make it a simple copy and paste. Am using Win Xp and Publisher 03. Your help is and always has been very much appreciated. Thanking you in ...

Pull Data From Multiple Tables ????
Hi I will have 4 tables name "TblCostomers","TblVendors","TblAccounts", TblExpenses" Now i have a for name "FrmDrVouchers" that has a table "TblDrVouchers" in source. This form has two TxtBox Control name "TxtAccountNo" and "TxtAccountName" If User enters a Account No., It pulls the Account Name from Any One of these Table. I can do this if I have only one table. But tell me how can i do it while I have 4 tables for One Field of a table Thank you.. -- Message posted via AccessMonster.com http://...

STATEMENT DOWNLOAD PROBLEMS #2
Hi, I am trying to download statements and keep coming up with an error which says that "A communication error has occurred. Please try or call again." I have tried deleting and recreating the OFX file extension but this hasn't helped. Does anyone have any ideas? Thanks Caz In microsoft.public.money, Cazzy123 wrote: >I am trying to download statements and keep coming up with an error which >says that "A communication error has occurred. Please try or call again." I >have tried deleting and recreating the OFX file extension but this hasn't >he...

Wacky migration wizard problems
Hello all, We have two separate domains with the following configuration: Single NT4 PDC running Exchange 5.5 Single W2003 DC running Exchange 2003 There is a two way trust between the domains. I ran the Migration Wizard and moved over one user account. I logged into the new domain as the user, setup Outlook with the proper settings, and got an error that the user account did not have permissions to the mailbox. I checked in system manager and Self has all the necessary rights to the mailbox. The weird thing is that we did another test migration a month ago with the same domains...

Outlook 2003 Add-in: Personal Folders Backup uninstall problem
Hello techie nerds out there Help! I am trying to uninstall this utility to reinstall it on another desktop on the same machine. It seems you can only run this utility on one desktop? Running windows XP pro and Outlook 2002 The programme does not show up in the Add/Remove programme window in the Control Panel. Many, many thanks Paul Paul Allen <ict@wandsworthvb.org.uk> wrote: > I am trying to uninstall this utility to reinstall it on > another desktop on the same machine. It seems you can only > run this utility on one desktop? Doesn't seem to be the case here. ...

"Find a contact box" problem
Hi. I�m having problems with the �Find a contact� box on the Standard toolbar. Previously, I would type in a name and the search would pull up name/s from the Outlook Contacts folder first then, if the name is not in the Contacts folder, it would pull up name/s from the Global Address List if any. Now, when I type in a name, the search only pulls up name/s from the Global Address List. The search does not include the Contacts folder although there are entries in the Contacts folder. The problem started after I transferred a couple of Contacts subfolders to another pst file. How do I fix the p...

Dummy series and data table
Hi, I have a chart that presents 2005, 2006, 2007 summary data as a column chart and then 2007 by month as a line. To show the yearly data I have a yearly category, after which I have individual months where the yearly data is zero - sort of like a dummy series - because I only have one value for them. The 2007 detailed data has zero in the yearly column but all the individual values in the monthly columns. It worked fine until I was asked to add a data table to the chart. Now, since it has 2007 twice - once as the summarized for the year and the other as all these individual months - some us...

Minimizing Rows of with similar data
I am using Excel 2007. I have 3 columns A, B, and C such as below: TU10-10 TU10 Tungsten Satin Wedding Band TU10-10.5 TU10 Tungsten Satin Wedding Band TU10-11 TU10 Tungsten Satin Wedding Band TU10-11.5 TU10 Tungsten Satin Wedding Band TU10-12 TU10 Tungsten Satin Wedding Band TU10-12.5 TU10 Tungsten Satin Wedding Band TU10-13 TU10 Tungsten Satin Wedding Band TU10-6 TU10 Tungsten Satin Wedding Band TU10-6.5 TU10 Tungsten Satin Wedding Band TU10-7 TU10 Tungsten Satin Wedding Band TU10-7.5 TU10 Tungsten Satin Wedding Band TU10-8 TU10 Tungsten Satin Wedding Band TU10-8.5 TU10 Tung...

Data Import/ Data Map Questions
Hello, I am developing a solution that will allow various new and updated entities to be loaded from several source systems into a Microsoft Dynamics 4.0 installation on an ongoing basis. There are some up- front components to the solution that detect new/changed source system data that are custom but I wanted to be able to use the built-in Data Import facilities within 4.0 to take the data from Excel files and load it into the CRM. I've looked at the sample code in the SDK and have been able to write code that will do this, including use of Picklist and Lookup (ie parent account) type ...

How do I get a copy of Publisher 2002?
I'd like to get a copy of Publisher 2002. I still have Windows 98 on my computer and don't want to upgrade to XP right at this time. Other than ordering off of e-bay or other sites I'm not familiar with, is it possible to get a copy of Publisher 2002 that would be compatible with Windows 98? Amazon.com -- Don Vancouver, USA "Crystal1365" <Crystal1365@discussions.microsoft.com> wrote in message news:33518C70-6D02-4E03-B9C2-2C1FDEEB4A68@microsoft.com... > I'd like to get a copy of Publisher 2002. I still have Windows 98 on my > computer and don...

Returning Data from a third cell in same row that meets two other
I'm looking for a formula that will return a result from a cell on a same row as two other cells that meet certain criteria. I'm sure there is a way to do this but i am a novice at this kind of stuff and can't seem to figure it out. For example i want a cell to = what is in column E when column A="36751" and when column B="Total Returns" The spreadsheet has 55000 rows. there will only be one instance where both these criteria are met. i want to use this to create a seperate spreadsheet with just info i need and can update on a daily basis. thanks in ...

Drag Drop problem in CListCtrl
Hello , I have a CListCtrl which displays the thumbnail view of image files & other related info like file size , destination.The owner drawn property of the list cntrl is set to true. I want to be able to drag - drop the thumbnails within the list view itself. But the dragged image of the selected list item is always displayed as a black bitmap.I have used CreateDragImage , BeginDrag , DragEnter , DragMove etc for this.Why is this problem ocurring???? Also if instead of having the dragged image of the item to be displayed , i have set a default bitmap, but it too doesn't get displa...

Save data before printing
Hello all, An acquiantence is programming in Excel 2003 and has a sheet where theuser enters receipt data. When he is done, he clicks a boton to copythe data from the sheet to another in tabular form withinthe same workbook. What he needs to do is to implement a lock so that the user can't print out the receipt until after he has clicked the boton. His button click event code is the following: Private Sub CommandButton1_Click() Application.ScreenUpdating = False 'Copy data to BD2 sheet Sheets("BD2").Unprotect With Sheets("BD2").Range("A1000&qu...

Format Problem
I have a report of a crosstab query. I formatted the left column of the report with Text Align set to Right. The report is chopping off the last character of the term, even if I set the Right Margin to 0.5". Doing so just puts space between the right edge of the border and the end of the chopped word I have played with all of the format settings in the properties with no success. If I set Text Align to Left the word is not chopped, but I would prefer Text Align set to right. Is there a way to fix this format issue with Text Align set to the Right? -- Message posted via http://www.acce...

Help Creating A Formula To Copy and Paste Text
Hi everyone. Thanks in advance for any help you may have. I need to create a formula that searches for a specific word in a column then cut the word and paste it to another column. Of course this would be simple if it was the only word in the column, but there is other text that I would like to remain in the orginal column.. Thanks again, Mary You have a reply at your other thread. Mary wrote: > > Hi everyone. Thanks in advance for any help you may have. > > I need to create a formula that searches for a specific word in a > column then cut the word and paste it to ...

copy quote
We have a common situation where we will generate a quote and then need to produce slight variations as seperate quotes. I would expect that it would be possible to write script or some other means that would basically perform a "quote copy". As I begin to look into the possibilities, I thought I would see what the others may suggest to accomplish this. Tim Ben Vollmer's blog has a great tutorial on cloning records that can be easily adapted to your need. Check it out at http://blogs.msdn.com/midatlanticcrm/archive/2006/04/10/Fun_with_JScript__Cloning_a_Case_aka_How_do_...

Time conversion problem
I am trying to write some data analysis macro code and am having problems with how Excel is and isn't storing the times. During a study a researcher enters, for example 4:39. That needs to be 4 minutes and 39 seconds. However, Excel formats it as 4 hours and 39 minutes - as "h:mm". I really want it formatted as "hh:mm:ss" so it would be 00:04:39. If I go in and change the cell format to "hh:mm:ss" it simply writes the value out as 4:39:00. I've tried switching back and forth between various date, general, and text formats to no avail. For a number of reas...

Synchronisation Problem with Outlook
Hi I am running Money 2004 on a PC under Windows XP Home with Service Pack 2 installed. I have recently invested in a HP iPAQ hx2490 and have it set up to synchronize through a USB cradle. Synchronization settings include contacts, calendar, notes, files and Microsoft Money data. All went well until 10th.Feb or thereabouts, when Microsoft Money Data stopped synchronizing, with the message "Attention Required" opposite the Microsoft Money listing. All other data continues to synchronize satisfactorily. HP support tell me that since other data continues to synchronize satisfactorily ...