Use Vba Pass Array To C++ Dll

  • Hi,all
    Ask a simple question,I want use vba pass array to c++ dll , and return another array back to vba
    example is below,but I return value always 0 , how should I do??


    vba code
    ===================================================


    =======================================================


    ==========================================================
    I want to establish pin() array 1 ~ 10 , pass to dll and return pout() array 100 ~ 1000
    Anyone konws how to revise,plz tell me
    Thanks a lot !!!!!

  • Re: Use Vba Pass Array To C++ Dll


    Quote from ByTheCringe2

    Welcome to the forum. However, please read the rules and use code tags for VBA. I have added them for you this time. Normally the post would be deleted.


    Sorry,I'll pay attention forum's rule next time.
    Thanks for your help.:)

  • Re: Use Vba Pass Array To C++ Dll


    I'm afraid I can't answer your specific question but I wonder why you want to do this in the first place.


    Is the C++ code posted just a simple example or is that what you actually want to do?


    If it's what you want to do then why not just do everything in VBA?

  • Re: Use Vba Pass Array To C++ Dll


    Quote from norie

    I'm afraid I can't answer your specific question but I wonder why you want to do this in the first place.


    Is the C++ code posted just a simple example or is that what you actually want to do?


    If it's what you want to do then why not just do everything in VBA?


    There may be some reasons for me to do this.


    Acturally,I want to generate a large array.
    As I know , VBA array has limit less than 30,000(roughly)
    C++ could generate more than 100,000
    and calculate speed also my concern. C++ is amazing quick.


    Second, dll protect code safty more than vba.
    vba password is so easy to be crack.

  • Re: Use Vba Pass Array To C++ Dll


    Been a loooonnnnggg time since I did any C but I don't see where your dll is (a) creating an array, or (b) actually returning it. Also, I think the function in excel should return an array.


    In C (with other stuff as requred)
    Long _stdcall myarray(Double *pin)
    pout[10] 'define the array
    for (i = 0; i<=10; i++) 'fill the array
    pout[i] = pid[i)]*100;
    return pout; 'return the array
    ...


    Not sure if the pid array will work this way, or how you will initialize any array etc.


    Your function would then be more like



    All that said, if the size of an array in Excel is approx 30k, and you want to build an array of about 100k, then it would be pointless as you would not be able to return, or action it (perhaps).


    On a limb here, and expect to get cut off due to lack of understanding of the C code....



    Tony

  • Re: Use Vba Pass Array To C++ Dll



    As you say , i try again


    double _stdcall myarray(Double *pin)
    double pout[10]; 'define the array
    for (i = 0; i<=10; i++) 'fill the array
    pout[i] = pid[i)]*100;
    return pout; 'return the array
    ^^^^^^^^^^^^^^^^^^
    compile have sime error "couldn't convert from double[10] to double"


    I have saw someone's vba code use like this

    Code
    1. declare function myarray(byval xin as double , byval yin as double , _
    2. byref zin() as double,byref xout as double ,byref yout as double, _
    3. byref zout() as double)


    first three parameter are input values ,
    last three parameter are output values.
    vba definite input values , and c++ calculate output values
    I think that it is easy way to use function.
    But I have no idea how to write dll code to do this.....:confused:

    You say that array can't be match may be right.
    I still can use c++ generate 100k random.and calculate average
    or standard deviation return to vba.

  • Re: Use Vba Pass Array To C++ Dll


    Hi


    What happens if you go back to your original structure, and change the line
    pout=pin*100;
    to
    pout[i]=pin*100;
    in the C++ code.


    If that doesn't do much, with the original code, try the msgbox line from
    msgbox pout(3)
    to
    msgbox pout(0).



    Tony

  • Re: Use Vba Pass Array To C++ Dll



    Hello ,ACW
    I try again and again.
    but can't worked.
    pout[ i ] = pin[ i ]*i has be revised.


    here is my c++ project and excel file
    http://www.savefile.com/files/432455
    Could someone can point out what's wrong
    If download file , vbadll folder should be placed on C:/
    If vba error message show "can't find dll"


    plz modify vba module code path
    " Declare Function myarray Lib "c:\vbadll\debug\vbadll.DLL"

  • Re: Use Vba Pass Array To C++ Dll


    do as follow


    vba:


    your c++ code


    .def file as before


    HTH


    filippo

  • Re: Use Vba Pass Array To C++ Dll



    It works!!!
    Thank you very much.
    Ya~~