Traduction d'un code C++ en C# pour récupérer des données encodées dans le Clipb

thegodone Messages postés 2 Date d'inscription mardi 29 mai 2007 Statut Membre Dernière intervention 13 juillet 2009 - 13 juil. 2009 à 08:15
krimog Messages postés 1860 Date d'inscription lundi 28 novembre 2005 Statut Membre Dernière intervention 14 février 2015 - 17 juil. 2009 à 10:13
Bonjour,

Je cherche à récupérer l'object CDX embedded dans Word afin d'extraire ces données dans une forme windows en c#.

Je n'ai pas réussi à obtenir un Cast sur le COM de chemdraw toujours des erreurs de type:
Unable to cast COM object of type 'System.__ComObject' to interface type 'ChemDraw.Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{4E53CB60-4A53-4CBD-BA5D-FF7CB17E8319}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Sur le site de Cambridgesoft propriétaire de ChemDraw j'ai trouvé ce lien:
http://www.cambridgesoft.com/services/documentation/sdk/chemdraw/cdx/Clipboard.htm

qui me dit qu'il est possible d'extraire du clipboard "le dit" CDX voici le code en c++:

#define kScrapCommentKind 100
#define kScrapIDSize 5
static void DoMyComment(short kind, short dataSize, unsigned char *theData, string *theCDX)
{
// It's ours if it has the right comment kind, and begins with the ScrapID.

if (commentKind != kScrapCommentKind)
return;

if (dataSize < kScrapIDSize)
return; // not long enough

if (memcmp(theData, "CDIF", kScrapIDSize) == 0);
theCDX->append(theData + kScrapIDSize, dataSize - kScrapIDSize);
}


static int Picture__EnumCommentsEMF(HDC theDC, HANDLETABLE *table, CONST ENHMETARECORD *mr, int nObj, LPARAM theCDX)
{
if (mr->iType == EMR_GDICOMMENT) // CSC records are escaped comments
{
int length = mr->nSize - 3 * sizeof(mr->dParm[0]);
if (length > 0 && theCDX != NULL)
{
unsigned char *theData = (unsigned char *)&mr->rdParm[1];
DoMyComment(kScrapCommentKind, length, theData, (string *)theCDX);
}
}
return 1;
}

string ExtractCDXFromPicture(HENHMETAFILE enhmetafile)
{
string theCDX;
RECT r = {0, 0, 9999, 9999};
BOOL res = EnumEnhMetaFile(NULL, enhmetafile, Picture__EnumCommentsEMF, (LPARAM)&theCDX, &r);
}

merci d'avance

Thegodone

3 réponses

thegodone Messages postés 2 Date d'inscription mardi 29 mai 2007 Statut Membre Dernière intervention 13 juillet 2009
13 juil. 2009 à 12:00
rebonjour,

Je viens de trouver un programme qui m'a donné une traduction approximative:

private static void DoMyComment(short kind, short dataSize, ref byte theData, ref string theCDX)
 { // It's ours if it has the right comment kind, and begins with the ScrapID.
 if (commentKind != DefineConstants.kScrapCommentKind)
    return;
if (dataSize < DefineConstants.kScrapIDSize)
   return; // not long enough
//C++ TO C# CONVERTER TODO TASK: The memory management function 'memcmp' has no equivalent in C#:
  if (memcmp(theData, "CDIF", DefineConstants.kScrapIDSize) == 0)
   ;
//C++ TO C# CONVERTER TODO TASK: The append method is not converted to C#:


   theCDX.append(theData + DefineConstants.kScrapIDSize, dataSize - DefineConstants.kScrapIDSize);
 }


private static int Picture__EnumCommentsEMF(IntPtr theDC, ref HANDLETABLE table, CONST ENHMETARECORD mr, int nObj, IntPtr theCDX)
{ // CSC records are escaped comments { int length = mr->nSize - 3 * sizeof(mr->dParm[0]);
 if (mr.iType == EMR_GDICOMMENT)
 if (length > 0 && theCDX != null)
 {
  byte theData = (byte) mr.rdParm[1];
  DoMyComment(DefineConstants.kScrapCommentKind, length, ref theData, ref (string)theCDX);
 }
}
private return 1 ;
}


 


private string ExtractCDXFromPicture(IntPtr enhmetafile)
{
 string theCDX;
 RECT r = new RECT(0, 0, 9999, 9999);
 int res = EnumEnhMetaFile(null, enhmetafile, Picture__EnumCommentsEMF, (IntPtr) theCDX, r);
}


Je ne trouve pas de fonctions equivalentes en c# pour la comparaison memcmp en mémoire et rien sur une méthode de modification de la chaine par un append comme en c++ avez vous des suggestion
0
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
13 juil. 2009 à 15:02
Salut, ce code > ici< c'est juste un exemple pour récuperer le contenu du clipboard, la classe managée Clipboard doit pouvoir faire la même chose.

Pour "memcmp" soit tu écris en code unsafe ta propre function soit tu utilises celle du run-time C :


[ DllImport( "msvcrt.dll", EntryPoint = "memcmp" ) ]
private static extern int MemoryCompare(
    IntPtr buf1, IntPtr buf2, IntPtr count );
[ DllImport( "msvcrt.dll", EntryPoint = "memcmp" ) ]
private static extern int MemoryCompare(
    byte[ ] buf1, byte[ ] buf2, IntPtr count );
0
krimog Messages postés 1860 Date d'inscription lundi 28 novembre 2005 Statut Membre Dernière intervention 14 février 2015 49
17 juil. 2009 à 10:13
Salut

Comme dit Lutinore, la classe managée Clipboard permet très simplement d'utiliser le presse-papier.

Exemples de méthodes utiles :
void Clipboard.Clear();
void Clipboard.SetText(string text);
bool Clipboard.ContainsText();
string Clipboard.GetText();

(tu as la même chose pour les fichiers, les images...)

Krimog : while (!(succeed = try())) ;
- NON, "LE BAR" n'est PAS un langage de programmation ! -
0
Rejoignez-nous