+ Reply to Thread
Results 1 to 8 of 8

use a function in a dll

  1. #1
    Registered User
    Join Date
    06-08-2012
    Location
    Paris, France
    MS-Off Ver
    Excel 2000
    Posts
    5

    use a function in a dll

    Hello,

    I'm french so I apologize in advance for my English...

    I have made a dll which contain a function in C. This function takes four parameters and returns three results, this is done with pointers :

    Please Login or Register  to view this content.
    I tested it with an application and it works.

    And now I want to use it in Excel (the aim is that the user enters the four parameters in four cells and see the three results in three other cells) but I have never used VBA before. I tried several things but none of them works.

    My last failed try looked like this :

    Please Login or Register  to view this content.
    My main problems are :
    -to know how to chose between a Function and a Sub (i think that a Function returns a result and a Sub don't)
    -to know how to manage with the pointers
    -to understand how to get back the three results (I tried to get back at least one in the previous example but ideally I would like to have the three "Aff_***")


    If anybody can explain to me how to solve my problem or can give me some advice, that would help me a lot !

    Thank you very much in advance

  2. #2
    Forum Contributor tkowal's Avatar
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: use a function in a dll

    I do not believe I can help you but possibly I can point you in the right direction.

    A function can only return one variable. One way of returning multiple items is by defining a User Define Type.
    eg.
    Please Login or Register  to view this content.
    This type then could be pass to your function Myfunction (mycar)
    The function would be defined
    Please Login or Register  to view this content.
    In a sense this is a pseudo class... the other way would be changing your function to a class or object and the params would be properties and methods...

    Hopefully this will encourage some new thoughts on your problem.
    Ted
    "Live Long and Prosper"

  3. #3
    Registered User
    Join Date
    06-08-2012
    Location
    Paris, France
    MS-Off Ver
    Excel 2000
    Posts
    5

    Re: use a function in a dll

    Ok, thank you for this helpfull hint about how to return multiple values.

    But I still have not solve the main problem which is :

    How can I call my Sub (or Function) Aff_Gaz declared from the C-function A2_Aff_lineique ? I can't call it without an exit value !


    eg, in :

    Please Login or Register  to view this content.
    my problem is the line
    Please Login or Register  to view this content.
    for several reasons :


    1. in C the function is definied by
    Please Login or Register  to view this content.
    so I can't have
    Please Login or Register  to view this content.
    can I ?

    2. in C, I have pointers on Aff_tot, Aff_02 and Aff_H20 but no value because the three of them are the results

    Please, help me if you can or tell me if my question is not clear...

    Thank you all

  4. #4
    Forum Contributor tkowal's Avatar
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: use a function in a dll

    Shows how to handle C structures passed as pointers

    http://msdn.microsoft.com/en-us/library/bb687915.aspx

  5. #5
    Registered User
    Join Date
    06-08-2012
    Location
    Paris, France
    MS-Off Ver
    Excel 2000
    Posts
    5

    Re: use a function in a dll

    Hello !

    I have had some answers and progressed a little.

    I tried another way to define my C-function :

    Please Login or Register  to view this content.

    became

    Please Login or Register  to view this content.

    and return 1 (only in order to make it easier to call in VBA, I don't know if it's a good idea but it's a try...)

    My VBA code is now :

    Please Login or Register  to view this content.

    But when I enter the four parameters in four cells and I make =Affaiblissement_Gaz(B2;B3;B4;B5) in the cell destinated to the result, Excel gives me #VALEUR! (#VALUE! in english I guess).


    I have no error message, so I can't find what is wrong...can you ?
    Thank you very much in advance for your help !

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,108

    Re: use a function in a dll

    Can you try calling the function in VBA rather than from a cell as that would allow you to step through and perhaps get an error message.
    Everyone who confuses correlation and causation ends up dead.

  7. #7
    Forum Contributor tkowal's Avatar
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: use a function in a dll

    Thanks romperstomper for the support -- this is a bit out of my arena!

    Granbapt, here is another link http://www.excelforum.com/excel-prog...ba-to-dll.html

    It references wrapping your dll call in a function and as romper indicated would allow you to step and test your calls through the debugger....

  8. #8
    Registered User
    Join Date
    06-08-2012
    Location
    Paris, France
    MS-Off Ver
    Excel 2000
    Posts
    5

    Re: use a function in a dll

    Thank you all, I've found the problem which was (as usual) not where I was looking, and now it works !!

    Indeed, my export of the function from C-code to DLL was wrong.

    To close the subject, and if you are interested, here is the way I've done for the VBA part of my question :
    (I took back my C-function in the initial form : with the void type)


    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1