Hi everyone,
I am a student,quite new to C++. I am working on my thesis right now. Here is my situation, I have an algorithm for my thesis topic and I tried making an example of this in C++ and it works fine with a console application. But I am doing this for a company with whom I am doing my internship and they already have an application this field. They want me to implement my algorithm in their application. This application has an option to call .dll file which I am planning to use. This application has some libraries which I can use. I also tried to make a small mathfunctions example and it works fine. but when I use for loops inside the dll file the program freezes and doesn't run anymore.

below is the skeleton of the .cpp file. I am supposed to write my code in the user program area. Another problem I am facing is the array data can be accessed only by calling pre defined functions from the lib file like getdata, setdata.. etc.

include "stdafx.h"
include <windows.h>
#include "stdafx.h"
#include <windows.h>
#include "C:\users\include\libpltUsrSafeArray.h "

int ADD(DWORD dwArgumentCount, VARIANT * pvArgument)
{
    HRESULT hr = S_OK;
    int rtn = 0;

    LPSAFEARRAY psfarray01 = NULL;  /* Input MTX(MATRIX01) */
    hr = ::SafeArrayCopy(pvArgument[0].parray, &psfarray01);
    double ddata02 = 0; /* Output VALUE01 */
    double ddata03 = 0; /* Output VALUE02 */
/***** User Program *****/
/* user program is written here*/

    PLTSArrayGetSize(psfarray01, rowsize, colsize); /*pre defined function to get array size */



/***** User Program *****/
    if( psfarray01 ) {
        ::SafeArrayDestroy( psfarray01 );
        psfarray01 = NULL;
    }
    pvArgument[1].dblVal = ddata02;
    pvArgument[1].vt = VT_R8;
    pvArgument[2].dblVal = ddata03;
    pvArgument[2].vt = VT_R8;
    return(rtn);
}

would be helpful if someone could suggest how to approach this.

We cannot help if we do not know what your function looks like :S I don't see any for-loops. I cannot tell from just reading that what the problem is. Perhaps you are accessing an invalid section in memory when trying to read/store the array?
That could be the cause of the crash perhaps.

But again, from looking at the above, we cannot even tell what is happening other than an array of variable types is being passed as an argument and stored all it's data to another array via a pointer?

Use a try catch or a step-through/variable watch to see what's going on.

Do some reading on how to load a DLL in a running program if your goal is to dynamically link your DLL to the program based on a program argument. I'll teach you how to fish. You have to go to the river to find them...

Sorry about not posting the complete code. I figured out the crash, it was due to the scan period set by the application which the dll is linked to. When I manipulated the scan period the crash didn't occur. But I have another question though. These functions are pre defined and I have to use them to link the program argument. I used the following way to acces the data in the array for calculations. Is this the only way? or Is there any simpler way other than calling the GetElement function evertime to read the data?

#include "stdafx.h"
#include <windows.h>
#include "C:\users\include\libpltUsrSafeArray.h "
int ADD(DWORD dwArgumentCount, VARIANT * pvArgument)
{
    HRESULT hr = S_OK;
    int rtn = 0;
    double** m_p_array;

    LPSAFEARRAY psfarray01 = NULL;  /* Input MTX(SYSTEMMATRIX) */
    hr = ::SafeArrayCopy(pvArgument[0].parray, &psfarray01);
    LPSAFEARRAY psfarray02 = NULL;  /* Output MTX(TESTMATRIX) */
/***** User Program *****/
/****** Please write here! *****/

    long rowsize, colsize;
    double tempdata;

    PLTSArrayGetSize(psfarray01, rowsize, colsize);

    m_p_array = new double*[rowsize];
    for(int k=0; k<rowsize; k++)
    {
        m_p_array[k]=new double[colsize];
    }

    for(int k=0; k<rowsize; k++)
    {for(int j=0; j<colsize; j++)
    {
        PLTSArrayGetDblElement(psfarray01, k, j, tempdata);
        m_p_array[k][j]= tempdata;
    }}




/***** User Program *****/
    if( psfarray01 ) {
        ::SafeArrayDestroy( psfarray01 );
        psfarray01 = NULL;
    }
    if( psfarray02 ) {
        if( pvArgument[1].parray ) {
            ::SafeArrayDestroy( pvArgument[1].parray );
            pvArgument[1].parray = NULL;
        }
        pvArgument[1].parray = psfarray02;
        pvArgument[1].vt = VT_ARRAY | VT_VARIANT;
    }
    return(rtn);
}

You shouldn't have to call that function every time. I "THINK" you can just iterate it just like a normal 2Darray OR use a pointer to iterate it.

Try it and see how it works out. If it doesn't work then I guess you are restricted to using that function for getting the elements.

I tried it. Both using pointers and as in 2D array, it doesn't work. I have to use the functions. Seems like this application in which I am running my dll in is not that flexible, it takes more than 5 seconds to run a dll and there is scan period for the application itself and when it exceeds there is error or it skips executing the dll. The program is something like a lab view, runs blocks after blocks. I am still going to continue to make a simple example in it and explain them that its not the right platform and present it as cpp in my university.

Thanks for your reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.