2010-04-22 3 views
0

J'ai essayé d'utiliser IDataObject pour mettre un peu de texte dans le presse-papiers. Mais le GlobalUnlock échoue. Qu'est-ce que je fais mal?IDataobject-> SetData a échoué

IDataObject *obj; 
HRESULT ret; 
assert(S_OK == OleGetClipboard(&obj)); 

FORMATETC fmtetc = {0}; 
fmtetc.cfFormat = CF_TEXT; 
fmtetc.dwAspect = DVASPECT_CONTENT; 
fmtetc.lindex = -1; 
fmtetc.tymed = TYMED_HGLOBAL; 

STGMEDIUM medium = {0}; 
medium.tymed = TYMED_HGLOBAL; 
char* str = "string"; 

medium.hGlobal = GlobalAlloc(GMEM_MOVEABLE+GMEM_DDESHARE, strlen(str)+1); 
char* pMem = (char*)GlobalLock(medium.hGlobal); 
strcpy(pMem,str); 
assert(GlobalUnlock(medium.hGlobal) != 0); // !!! ERROR 
assert(S_OK == obj->SetData(&fmtetc,&medium,TRUE)); 
assert(S_OK == OleSetClipboard(obj)); 
+0

Eh bien, ce qui est GetLastError() quand GlobalUnlock() échoue? Etes-vous sûr que GlobaAlloc() et GlobalLock() réussissent? – Luke

+0

GetLastError ne contient aucune information en cas d'échec. Et, le alloc/lock n'a aucun problème. – trudger

Répondre

0

Eh bien, après avoir regardé le documentation est d'attendre:

Valeur de retour

Si l'objet de la mémoire est toujours verrouillé après décrémenter le nombre de verrouillage, la valeur de retour est un valeur non nulle. Si l'objet de mémoire est déverrouillé après la décrémentation du nombre de verrous, la fonction renvoie zéro et GetLastError renvoie NO_ERROR.

Si la fonction échoue, la valeur de retour est zéro et GetLastError renvoie une valeur autre que NO_ERROR.

Ainsi, votre affirmation est fausse; il devrait être:

assert(GlobalUnlock(medium.hGlobal) == 0 && GetLastError() == NO_ERROR); 
+0

Comme je suis stupide, je l'ai corrigé. Mais le SetData échoue toujours. Il retourne E_FAIL et GetLastError renvoie 0. En fait, je peux utiliser OleGetClipboard pour obtenir l'interface pour writting? – trudger

+0

j'ai finalement décidé d'utiliser l'API Windows SetClipboardData. C'est plus facile. – trudger

Questions connexes