Click here to Skip to main content
16,008,490 members
Home / Discussions / COM
   

COM

 
Questionactivex problems... Pin
l a u r e n15-Sep-05 9:28
l a u r e n15-Sep-05 9:28 
GeneralRe: activex problems... Pin
Jörgen Sigvardsson15-Sep-05 9:57
Jörgen Sigvardsson15-Sep-05 9:57 
GeneralRe: activex problems... Pin
l a u r e n15-Sep-05 10:00
l a u r e n15-Sep-05 10:00 
GeneralRe: activex problems... Pin
rwestgraham15-Sep-05 10:11
rwestgraham15-Sep-05 10:11 
GeneralRe: activex problems... Pin
l a u r e n15-Sep-05 10:15
l a u r e n15-Sep-05 10:15 
GeneralRe: activex problems... Pin
Jörgen Sigvardsson16-Sep-05 7:10
Jörgen Sigvardsson16-Sep-05 7:10 
GeneralRe: activex problems... Pin
l a u r e n16-Sep-05 8:04
l a u r e n16-Sep-05 8:04 
GeneralRe: activex problems... Pin
rwestgraham16-Sep-05 14:24
rwestgraham16-Sep-05 14:24 
Not dumb, this stuff is complicated. I tested the below code in VC++6/VB6 and it worked.

ATL/C++ Server Code:

***************************************************************************

STDMETHODIMP CVBArray::GetVBArray(VARIANT *MyArray)
{

//Create a sample byte array and initialize with zeros
//256 is small, so don't bother creating on the heap
unsigned char bytearray[256] = {0};

//Declare a pointer to the byte array
unsigned char* pbytearray = bytearray;

//Set every other byte to 1
for(int i = 0; i <256; i+=2)
{
bytearray[i] = 1;
}

//Now comes the fun part!!!!

//Call VariantInit to initialize our return parameter
VariantInit(MyArray);
//Set the type of the return paramter to VT_UI1
//VB will recognize VT_UI1 as an unsigned char i.e. byte
MyArray->vt = VT_ARRAY| VT_UI1;

//Declare a pointer for a SAFEARRAY
SAFEARRAY* psa;
//Declare a SAFEARRAYBOUND and initialize it to the correct size
SAFEARRAYBOUND bounds = {sizeof(bytearray), 0};

//Create the SAFEARRAY using the type, dimension, and array length
psa = SafeArrayCreate(VT_UI1, 1, &bounds);

//Declare a null pointer for access to the SAFEARRAY's array
void* pSafeArrayData = NULL;

//Now set the pointer to the SAFEARRAY array (must be cast as void**)
SafeArrayAccessData(psa, (void**)(&pSafeArrayData));

//Let's review: We now have a local byte array populated with data.
//We also now have a SAFEARRAY object with a data buffer the size of our byte array.
//We have created a pointer for the SAFEARRAY's data buffer.
//We have "unlocked" the SAFEARRAY's data buffer and set our pointer to it
// by calling SafeArrayAccessData.

//Now all we need to do is copy our local data into the SAFEARRAY data buffer
// the clean up and return.

//Copy bytearray into SAFEARRAY buffer
memcpy(pSafeArrayData, pbytearray, sizeof(bytearray));
pbytearray = NULL;

//"Lock" the SAFEARRAY.
SafeArrayUnaccessData(psa);
//Set the SAFEARRAY we just created and populated to the return parameter.
MyArray->parray = psa;

//Now we are ready to return

return S_OK;
}


**************************************************************************

VB Code:

**************************************************************************

Private Sub GetArrayFromCOM()
Dim oServer As New vbArray
Dim testItems() As Byte
Dim i As Long
Dim count As Long
Dim strShow As String

testItems = oServer.GetVBArray
count = UBound(testItems)
For i = 0 To count
strShow = strShow & " " & CStr(testItems(i))
Next i

MsgBox strShow

End Sub

*****************************************************************************
GeneralRe: activex problems... Pin
l a u r e n19-Sep-05 6:20
l a u r e n19-Sep-05 6:20 
GeneralRe: activex problems... Pin
Jörgen Sigvardsson16-Sep-05 14:38
Jörgen Sigvardsson16-Sep-05 14:38 
GeneralRe: activex problems... Pin
rwestgraham16-Sep-05 15:19
rwestgraham16-Sep-05 15:19 
GeneralRe: activex problems... Pin
Jörgen Sigvardsson17-Sep-05 9:31
Jörgen Sigvardsson17-Sep-05 9:31 
GeneralRe: activex problems... Pin
rwestgraham17-Sep-05 12:05
rwestgraham17-Sep-05 12:05 
QuestionNeed help on problem with Excel processes Pin
J. Holzer14-Sep-05 3:44
J. Holzer14-Sep-05 3:44 
QuestionCOM newbie: LNK2005 Pin
Jose M Castellanos12-Sep-05 3:09
Jose M Castellanos12-Sep-05 3:09 
AnswerRe: COM newbie: LNK2005 Pin
Jose M Castellanos13-Sep-05 1:25
Jose M Castellanos13-Sep-05 1:25 
QuestionHow to Show ActiveX Control on a Window? Pin
Ashwin kumar Gurujala11-Sep-05 23:30
Ashwin kumar Gurujala11-Sep-05 23:30 
QuestionActiveX control container Pin
code_chenyf8-Sep-05 14:36
code_chenyf8-Sep-05 14:36 
QuestionUpdate (Windows) Pin
Illegal Operation8-Sep-05 4:10
Illegal Operation8-Sep-05 4:10 
QuestionSTA &amp; MTA Pin
MailtoGops8-Sep-05 3:07
MailtoGops8-Sep-05 3:07 
QuestionExcel Automation problem Pin
HeartFriend7-Sep-05 20:22
HeartFriend7-Sep-05 20:22 
QuestionCOM and access rights (perhaps ?) Pin
gnilk6-Sep-05 2:17
gnilk6-Sep-05 2:17 
QuestionHow to properly stop STA thread Pin
ComplexLifeForm3-Sep-05 7:19
ComplexLifeForm3-Sep-05 7:19 
QuestionATL Service Pin
Ashwin kumar Gurujala31-Aug-05 21:42
Ashwin kumar Gurujala31-Aug-05 21:42 
AnswerRe: ATL Service Pin
logicaldna31-Aug-05 22:47
logicaldna31-Aug-05 22:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.