I have a problem in retreiving data using SAFEARRAYS,when I am sending data from Visual Basic application to C DLLs.
I have a typical situation as below
I have defined following structures in VisualBasic application

Type EmployeeBonus
    emp_Bonus as double
End type
Type EmployeeSalary
    emp_salary as double
    empbonus() as  EmployeeBonus
End type
Type employee
    emp_Number as double
    empsalary()as EmployeeSalary
end type

my_employee() as employee
'I am taking care of all structure memory management in VB application.
                      My Sample VB Code
I am sending Employee structure to c DLL by calling
Dll function say:
'Dll already defined in say emp.bas file
'dll returning long value
ret = EmployeeCalculation(hwnd as long,ByRef My_employee() as employee)
When I am sending this employee Nested structure to C Dll
program, I could only able to retrieve data up to first nested structure in c program.(employee to employeesalary )
But when I am going to 2nd(from employeesalary to employeebonus) nesting level it is giving
Junk value.(i.e Memory allocation for 2nd structure is not done)
This  metod described in VB dovumentation file c:\program file\visual basic\vb4dll.txt
                   My Sample C code
case 1: I ensure size of structures are same when sending   data from VB to C Dll
case 2: Even if their is any remote possiblity of size miss match(which I think next to impossible) I prevented it by using padding.
struct employeebonus {
    int emp_Bonus;
    int padding;
struct EmployeeSalary{
   int emp_salary ;
   int padding;
   SAFEARRAY FAR* empbonus;
struct employee{
    int emp_Number;
    int padding;
    SAFEARRAY FAR* empsalary;
/*****************************C Dll call*******************/
__declspec(dllexport)int EmployeeCalculation(HWND hwnd,LPSAFEARRAY FAR *p)
strct employee as temp;
struct employeeSalary as empsal;
struct employeebonus as empbonus;
//Say If I do any mathematical manuplation here I could get //it in VB application
empsal.emp_salary += empsal.emp_salary ;
//Here I am getting Junk(unintilized )values
/**************For Information***********/
I am using VC++4.0 compiler for dll.

                             With Regards


Who is Participating?
mrmickConnect With a Mentor Commented:
ukoundinya, in my earlier suggestion, article number 5 covers SafeArrays.

It's title is "The Safe OLE Way of Handling Arrays"; however, this isn't OLE, it's OLE's way - which is exactly the same as VB.  If you look a little closer, you'll find it has a complete explanation of how to use SafeArrays.  Take another look.
Could it be related to the fact you are getting the element into empsal and then referencing it as tempsal? Or was that just a typo? (You could well have a tempsal somewhere else in your program that is being used without a compiler error).
ukoundinyaAuthor Commented:
I feel you are refferencing to the entier different situation.
Here when we use SafeArrays,and retrieve the element of
Safe Arrays it should be stored in temporary structures.
It is not for Syntax satisfaction......
                                                        Thanking You
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Here, translated into VB, is what you are doing:

temp = employee(p)
empsal = temp.empsalary(j)
empbonus = tempsal.empbonus(j)

but - tempsal has never been initialized! It is NOT the same as empsal in the code you posted.

My point is, that the code you posted simply and obviously will not work. Either the bug I pointed out is the reason it doesn't work, or you posted the code with mistakes. If you don't post the code exactly as it is written the chances of finding why it fails are low.
This is not an easy question, in fact, it's a very hard one.  Check out:

There are 5 articles that talk about writing DLLs for VB.  An explanation of SafeArrays is included.  In addition, each section contains a link that you can click to download sample source code (the download file is the same for all 5 articles).  You should be able to find your answer there.

ukoundinyaAuthor Commented:
Dear mrmick
I am very glad that you have suggested me good internet site.
   I have gone through the web documents.But they never suggested    how to solve for nested structures.
   Thanking You

How much do you expect for 50 points?  Common point values at Experts-Exchange are:

25: Very Easy
50: Easy
100: Medium
150: and up Hard

Do you think this is in the Easy catagory?
ukoundinyaAuthor Commented:
I am increasing the value of the question to 150.
I am interesed in the answer as i am facing very difficulty
in my project.
Thanking you
with regards

ukoundinya, I told you in the very first answer (which you rejected) why the code you *posted* doesn't work. If this isn't the problem, then you apparently don't want to post the actual code which is failing, and you don't want general references as to how to make safearrays work, so you might as well simply give up and stop wasting all our time. You can't ask for help then refuse to work with the people trying to help you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.