User Defined Types in Safearrays 
Author Message
 User Defined Types in Safearrays

Hello,

I'm having a problem using a Safearray of user defined types!
I used the msdn sample: "Passing Safearray of UDTs" has my guide, and in my
example
the safearray is passed has a an out parameter.
But when i pass the array of with UDTs for the first time an error occurs on
the client side and the
data is filled with garbage. But trying to obtain the data a second time,
this time, it works perfectly, has do
the following calls!!!
In conclusion, the error only happens once in the first time the method is
called, the error is on the client side reporting some kind of marshaller
error.
So my question what i'm i doing wrong!?!?

Thanks
Cludio Albuquerque



Sat, 11 Sep 2004 00:10:20 GMT  
 User Defined Types in Safearrays
Hard to tell what you are doing wrong, seeing as you don't show a single
line of code. Anyway, http://www.mvps.org/vcfaq/com/4.htm may be of
interest.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Quote:

> Hello,

> I'm having a problem using a Safearray of user defined types!
> I used the msdn sample: "Passing Safearray of UDTs" has my guide, and
in my
> example
> the safearray is passed has a an out parameter.
> But when i pass the array of with UDTs for the first time an error
occurs on
> the client side and the
> data is filled with garbage. But trying to obtain the data a second
time,
> this time, it works perfectly, has do
> the following calls!!!
> In conclusion, the error only happens once in the first time the
method is
> called, the error is on the client side reporting some kind of
marshaller
> error.
> So my question what i'm i doing wrong!?!?

> Thanks
> Cludio Albuquerque



Sat, 11 Sep 2004 00:57:49 GMT  
 User Defined Types in Safearrays
No problem, initialy i build up my own code, but now i'm just trying to make
the msdn sample work.
--------IDL
library Student
{
importlib("stdole2.tlb");
typedef enum tagclassification {school_Bully, class_Clown,
teachers_Favorite} classification;
typedef [uuid(D8B3861A-74C6-11d2-A0D6-00C04FB17CDB)] struct tagStudentStruct
{
      BSTR name;
      short grade;
      classification  type;
      VARIANT_BOOL   graduate;
Quote:
} StudentStruct;

[
object,
uuid(D50BD660-7471-11d2-9A80-006097DA88F5),         // IID_IStudent
helpstring("User Defined Data Server"),
dual,
pointer_default(unique)
]
interface IStudent : IDispatch
{
import "unknwn.idl";
typedef IStudent* LPSTUDENT;

HRESULT Test([in,out] SAFEARRAY(StudentStruct));

Quote:
};

--------Server Side
LPTYPEINF pTypeInfo = NULL;
LPTYPELIB pTypelib = NULL;
LPSAFEARRAY psaStudent = NULL;
SAFEARRAYBOUND rgbounds = { 4, 0 };
StudentStruct *pStudentStruct = NULL;
IRecordInfo* pRecInfo = NULL;

// Fetch the IRecordInfo interface describing the UDT
hr = LoadRegTypeLib(LIBID_Student, 1, 0, GetUserDefaultLCID(), &pTypelib);
hr = pTypelib->GetTypeInfoOfGuid(UUID_StudentStruct, &pTypeInfo);
_ASSERT(SUCCEEDED(hr) && pTypeInfo);
hr = GetRecordInfoFromTypeInfo(pTypeInfo, &pRecInfo);
_ASSERT(SUCCEEDED(hr) && pRecInfo);
RELEASEINTERFACE(pTypeInfo);
RELEASEINTERFACE(pTypelib);
psaStudent = SafeArrayCreateEx(VT_RECORD, 1, &rgbounds, pRecInfo);
RELEASEINTERFACE(pRecInfo);
hr = SafeArrayAccessData(psaStudent,
reinterpret_cast<PVOID*>(&pStudentStruct));

pStudentStruct[0].grade = 3;
pStudentStruct[0].name = SysAllocString(L"Shaun");
pStudentStruct[0].type = class_Clown;
pStudentStruct[1].grade = 8;
pStudentStruct[1].name = SysAllocString(L"Fred");
pStudentStruct[1].type = school_Bully;
pStudentStruct[2].grade = 12;
pStudentStruct[2].name = SysAllocString(L"Steve");
pStudentStruct[2].type = teachers_Favorite;
pStudentStruct[3].grade = 3;
pStudentStruct[3].name = SysAllocString(L"Linda");
pStudentStruct[3].type = teachers_Favorite;
hr = SafeArrayUnaccessData(psaStudent);
_ASSERT(SUCCEEDED(hr));
------------Client side
hr = pStudent->Test2(psaStudent);

Quote:
> Hard to tell what you are doing wrong, seeing as you don't show a single
> line of code. Anyway, http://www.mvps.org/vcfaq/com/4.htm may be of
> interest.
> --
> With best wishes,
>     Igor Tandetnik

> "For every complex problem, there is a solution that is simple, neat,
> and wrong." H.L. Mencken



Sat, 11 Sep 2004 03:35:10 GMT  
 User Defined Types in Safearrays
Does it work if you remove the typedef for the struct?

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

Quote:

> No problem, initialy i build up my own code, but now i'm just trying to make
> the msdn sample work.
> --------IDL
> library Student
> {
> importlib("stdole2.tlb");
> typedef enum tagclassification {school_Bully, class_Clown,
> teachers_Favorite} classification;
> typedef [uuid(D8B3861A-74C6-11d2-A0D6-00C04FB17CDB)] struct tagStudentStruct
> {
>       BSTR name;
>       short grade;
>       classification  type;
>       VARIANT_BOOL   graduate;
> } StudentStruct;
> [
> object,
> uuid(D50BD660-7471-11d2-9A80-006097DA88F5),         // IID_IStudent
> helpstring("User Defined Data Server"),
> dual,
> pointer_default(unique)
> ]
> interface IStudent : IDispatch
> {
> import "unknwn.idl";
> typedef IStudent* LPSTUDENT;

> HRESULT Test([in,out] SAFEARRAY(StudentStruct));
> };
> --------Server Side
> LPTYPEINF pTypeInfo = NULL;
> LPTYPELIB pTypelib = NULL;
> LPSAFEARRAY psaStudent = NULL;
> SAFEARRAYBOUND rgbounds = { 4, 0 };
> StudentStruct *pStudentStruct = NULL;
> IRecordInfo* pRecInfo = NULL;

> // Fetch the IRecordInfo interface describing the UDT
> hr = LoadRegTypeLib(LIBID_Student, 1, 0, GetUserDefaultLCID(), &pTypelib);
> hr = pTypelib->GetTypeInfoOfGuid(UUID_StudentStruct, &pTypeInfo);
> _ASSERT(SUCCEEDED(hr) && pTypeInfo);
> hr = GetRecordInfoFromTypeInfo(pTypeInfo, &pRecInfo);
> _ASSERT(SUCCEEDED(hr) && pRecInfo);
> RELEASEINTERFACE(pTypeInfo);
> RELEASEINTERFACE(pTypelib);
> psaStudent = SafeArrayCreateEx(VT_RECORD, 1, &rgbounds, pRecInfo);
> RELEASEINTERFACE(pRecInfo);
> hr = SafeArrayAccessData(psaStudent,
> reinterpret_cast<PVOID*>(&pStudentStruct));

> pStudentStruct[0].grade = 3;
> pStudentStruct[0].name = SysAllocString(L"Shaun");
> pStudentStruct[0].type = class_Clown;
> pStudentStruct[1].grade = 8;
> pStudentStruct[1].name = SysAllocString(L"Fred");
> pStudentStruct[1].type = school_Bully;
> pStudentStruct[2].grade = 12;
> pStudentStruct[2].name = SysAllocString(L"Steve");
> pStudentStruct[2].type = teachers_Favorite;
> pStudentStruct[3].grade = 3;
> pStudentStruct[3].name = SysAllocString(L"Linda");
> pStudentStruct[3].type = teachers_Favorite;
> hr = SafeArrayUnaccessData(psaStudent);
> _ASSERT(SUCCEEDED(hr));
> ------------Client side
> hr = pStudent->Test2(psaStudent);

> > Hard to tell what you are doing wrong, seeing as you don't show a single
> > line of code. Anyway, http://www.mvps.org/vcfaq/com/4.htm may be of
> > interest.
> > --
> > With best wishes,
> >     Igor Tandetnik

> > "For every complex problem, there is a solution that is simple, neat,
> > and wrong." H.L. Mencken



Sat, 11 Sep 2004 04:04:32 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Interface returns user defined type???

2. Use of enums for user defined types

3. User Defined Data Types and Pointers

4. HELP! user-defined data types

5. User defined types

6. User defined Type in ATL

7. Returning user defined data types

8. Convert C++ (return type int, long, user-define, etc) function into COM/ATL

9. Passing User defined data Type in ATL

10. An array of user defined types

11. Passing user defined type var between VB and VC

12. How to pass UDTs(User Defined Type Array)

 

 
Powered by phpBB® Forum Software