Posted on 1997-12-20
Last Modified: 2008-02-01
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


Question by:ukoundinya
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3

Expert Comment

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).

Author Comment

ID: 1446295
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

Expert Comment

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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 1446297
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.


Author Comment

ID: 1446298
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

Expert Comment

ID: 1446299

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?

Author Comment

ID: 1446300
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


Accepted Solution

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.

Expert Comment

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.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hide vba in gp 7 115
VBA Shell can't Find Word document 11 131
Advice in Xamarin 21 109
Macro problems with Excel file 6 26
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 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