• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 717
  • Last Modified:

SafeArrays

Sir
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;
SafeArrayGetElement(*p,&i,&temp);
SafeArrayGetElement(temp.empsalary,&j,&empsal);
//Say If I do any mathematical manuplation here I could get //it in VB application
empsal.emp_salary += empsal.emp_salary ;
SafeArrayGetElement(tempsal.empbonus,&j,&empbonus);
//Here I am getting Junk(unintilized )values
SafeArrayPutElement(tempsal.empbonus,&j,&empbonus);
SafeArrayPutElement(temp.empsalary,&j,&empsal);
SafeArrayPutElement(*p,&i,&temp);
/**************For Information***********/
I am using VC++4.0 compiler for dll.

                             ThankingYou
                             With Regards
                              V.C.Vasista

   

0
ukoundinya
Asked:
ukoundinya
  • 3
  • 3
  • 3
1 Solution
 
alamoCommented:
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).
0
 
ukoundinyaAuthor Commented:
Sir
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
0
 
alamoCommented:
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
mrmickCommented:
This is not an easy question, in fact, it's a very hard one.  Check out:

http://www.microsoft.com/oledev/olecom/cpp4vb.htm

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.


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

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?
0
 
ukoundinyaAuthor Commented:
mrmick
I am increasing the value of the question to 150.
Sir
I am interesed in the answer as i am facing very difficulty
in my project.
Thanking you
with regards
V.C.Vasista


0
 
mrmickCommented:
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.
0
 
alamoCommented:
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now