Solved

SafeArrays

Posted on 1997-12-20
9
673 Views
Last Modified: 2008-02-01
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
Comment
Question by:ukoundinya
  • 3
  • 3
  • 3
9 Comments
 
LVL 6

Expert Comment

by:alamo
ID: 1446294
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
 

Author Comment

by:ukoundinya
ID: 1446295
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
 
LVL 6

Expert Comment

by:alamo
ID: 1446296
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
 
LVL 8

Expert Comment

by:mrmick
ID: 1446297
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ukoundinya
ID: 1446298
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
 
LVL 8

Expert Comment

by:mrmick
ID: 1446299
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
 

Author Comment

by:ukoundinya
ID: 1446300
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
 
LVL 8

Accepted Solution

by:
mrmick earned 150 total points
ID: 1446301
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
 
LVL 6

Expert Comment

by:alamo
ID: 1446302
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now