 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jason Stipes Guest
|
Posted: Thu Apr 01, 2004 1:55 pm Post subject: How can I convert a struct to a SAFEARRAY, Variant or BSTR? |
|
|
I'd like to transmit an arbitrary data structure through a standard COM
interface as a standard type. I need a way to convert the struct to a
SAFEARRAY, Variant or BSTR. Essentially I want to take the block of memory
and put it into one of these data types so that I can directly get the data
out at the other end. So far BSTR has been a failure and using memcpy
hasn't worked. Has anyone done this before?
Thanks,
Jason
|
|
| Back to top |
|
 |
BigStew Guest
|
Posted: Thu Apr 01, 2004 4:03 pm Post subject: Re: How can I convert a struct to a SAFEARRAY, Variant or BS |
|
|
A few years ago I wrote a little test app to do just this.
A COM server passes a struct to the client using a VARIANT.
Code cut/paste from Builder 5 project.
// Server code
#include <safearry.h>
////////////////////////////////////////////////////////////////////////////
/
// TTestDCOMImpl
struct numstruct
{
int num1;
int num2;
int num3;
};
STDMETHODIMP TTestDCOMImpl::GetNums(TVariant* Nums)
{
numstruct n;
n.num1 = 101;
n.num2 = 202;
n.num3 = 303;
TSafeArrayDim1 dim(sizeof(numstruct));
TSafeArrayChar1 sa(dim);
for(int i=0; i<sizeof(numstruct); ++i)
sa[i] = ((char*)(&n))[i];
*Nums = sa.Detach();
return S_OK;
}
// Client code
struct numstruct
{
int num1;
int num2;
int num3;
};
void __fastcall TForm1::DisplayNums()
{
TVariant nums = Server->GetNums();
TSafeArrayChar1 sa = LPSAFEARRAY(nums);
numstruct n;
for(int i=0; i<sizeof(numstruct); ++i)
((char*)(&n))[i] = sa[i];
AnsiString s;
s = "Server says ";
s += IntToStr(n.num1);
s += ", ";
s += IntToStr(n.num2);
s += ", ";
s += IntToStr(n.num3);
s += ".";
Label1->Caption = s;
}
HTH
Stew
"Jason Stipes" <jason.stipes (AT) jhuapl (DOT) edu> wrote
| Quote: | I'd like to transmit an arbitrary data structure through a standard COM
interface as a standard type. I need a way to convert the struct to a
SAFEARRAY, Variant or BSTR. Essentially I want to take the block of
memory
and put it into one of these data types so that I can directly get the
data
out at the other end. So far BSTR has been a failure and using memcpy
hasn't worked. Has anyone done this before?
|
|
|
| Back to top |
|
 |
Jason Stipes Guest
|
Posted: Thu Apr 01, 2004 5:24 pm Post subject: Re: How can I convert a struct to a SAFEARRAY, Variant or BS |
|
|
Thanks, that did the trick.
-Jason
"BigStew" <Big_Stew (AT) talk21 (DOT) com> wrote
| Quote: | A few years ago I wrote a little test app to do just this.
A COM server passes a struct to the client using a VARIANT.
Code cut/paste from Builder 5 project.
// Server code
#include <safearry.h
////////////////////////////////////////////////////////////////////////////
/
// TTestDCOMImpl
struct numstruct
{
int num1;
int num2;
int num3;
};
STDMETHODIMP TTestDCOMImpl::GetNums(TVariant* Nums)
{
numstruct n;
n.num1 = 101;
n.num2 = 202;
n.num3 = 303;
TSafeArrayDim1 dim(sizeof(numstruct));
TSafeArrayChar1 sa(dim);
for(int i=0; i
sa[i] = ((char*)(&n))[i];
*Nums = sa.Detach();
return S_OK;
}
// Client code
struct numstruct
{
int num1;
int num2;
int num3;
};
void __fastcall TForm1::DisplayNums()
{
TVariant nums = Server->GetNums();
TSafeArrayChar1 sa = LPSAFEARRAY(nums);
numstruct n;
for(int i=0; i<sizeof(numstruct); ++i)
((char*)(&n))[i] = sa[i];
AnsiString s;
s = "Server says ";
s += IntToStr(n.num1);
s += ", ";
s += IntToStr(n.num2);
s += ", ";
s += IntToStr(n.num3);
s += ".";
Label1->Caption = s;
}
HTH
Stew
"Jason Stipes" <jason.stipes (AT) jhuapl (DOT) edu> wrote in message
news:406c1f53$3 (AT) newsgroups (DOT) borland.com...
I'd like to transmit an arbitrary data structure through a standard COM
interface as a standard type. I need a way to convert the struct to a
SAFEARRAY, Variant or BSTR. Essentially I want to take the block of
memory
and put it into one of these data types so that I can directly get the
data
out at the other end. So far BSTR has been a failure and using memcpy
hasn't worked. Has anyone done this before?
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|