c++ - Delphi WideString in Visual Studio MFC -


i have trying receive information exe written in delphi. exe passes me pointer 1 of data structures:

type   recordtype = record     st:   widestring;     next: pointer;   end;  var   datarec: ^recordtype; 

so in visual studio mfc have declared data type should similar:

struct recordrec {   bstr st;   void *next; };  recordrec *datarec; 

the delphi says widestring compatible bstr, however, not work. when @ st in debug mode says

"0x0000000000000000 <bad ptr>   wchar_t *" 

i don't know how declare equivalent of widestring in visual studio mfc.

if shortstring declare:

struct recordrec {   byte stlen;   char st[255];   void *next; }; 

but not work widestring , don't think should declare variable ~2^30 (1,073,741,824) characters in it.

what missing? hope can help.

a delphi widestring indeed wrapper bstr, however, not mean raw bstr pointer can passed as-is 1 process another. data has marshalled when passing across process boundaries. com handles automatically, passing raw bstr pointer manually not.

so, if can't change delphi app provide ipc-safe data block character data (similar shortstring workaround), receiving app have marshal bstr data manually. can use readprocessmemory() that:

  • read bstr's length within delphi app's address space (a bstr's character data prefixed 4-byte integer specifying length in wchar_t elements)
  • allocate wchar_t[] array of specified length within own address space
  • read character data delphi app's address space array in own address space.

for example (error handling omitted brevity):

recordrec *datarec = ...; std::wstring datast;  if (datarec->st) {     handle hprocess = openprocess(process_vm_read, false, thedelphiappprocessid);     int len = 0;     size_t numread = 0;     readprocessmemory(hprocess, lpbyte(datarec->st)-4, &len, 4, &numread);     if (len > 0)     {         datast.resize(len);         readprocessmemory(hprocess, datarec->st, &datast[0], len*2, &numread);     }     closehandle(hprocess); }  // use datast needed... 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -