View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.excel.sdk
Rob Y Rob Y is offline
external usenet poster
 
Posts: 8
Default Passing a string to excel from a C app

The working C++ code sample I'm copying from passes strings as VT_BSTR,
and it uses the SysAllocString method of CString to convert the C
strings into BSTR's for passing. Unfortunately, I can't seem to find a
C equivalent of CString::SysAllocString().

BSTR b;
b = SysAllocString(lpsz);
parm1.vt = VT_BSTR;
parm1.bstrVal = b;


Got it to work. Turns out SysAllocString does work from C. It's just
that it needs a multi-byte char string as input (I guess the C++ version
has a variant that take and converts a regular char string. So in C,
the conversion has to happen in 2 phases. This code works:

BSTR b;
LPCWSTR ws;
int len;

len = strlen(szStr) + 1;
ws = (LPCWSTR) malloc(len*2);
MultiByteToWideChar(CP_ACP, 0, szStr, len, ws, len*2);
b = SysAllocString(ws);
parm1.vt = VT_BSTR;
parm1.bstrVal = b;
free(ws);