need help in Vc++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rajini
    New Member
    • Aug 2007
    • 1

    need help in Vc++

    Hai....



    i have Vc++ code . i need to create Dll file by using following code
    can u help me....here with this i had sent my vc++ code
    [code=cpp]
    VARIANT root[64] = {0}; // Generic IDispatchs
    VARIANT parm[64] = {0}; // Generic Parameters
    VARIANT rVal = {0}; // Temporary result holder
    int level=0; // Current index into root[]

    // Initialize the OLE Library...
    OleInitialize(N ULL);

    // Line 1: dim app as object
    VARIANT app = {0};

    // Line 2: set app = createobject Excel.Applicati on
    {
    CLSID clsid;
    CLSIDFromProgID (L"Excel.Applic ation", &clsid);
    HRESULT hr = CoCreateInstanc e(clsid, NULL, CLSCTX_LOCAL_SE RVER|CLSCTX_INP ROC_SERVER, IID_IDispatch, (void **)&rVal.pdispV al);
    if(FAILED(hr)) {
    char buf[256];
    sprintf(buf, "CoCreateInstan ce() for \"Excel.Applica tion\" failed. Err=%08lx", hr);
    ::MessageBox(NU LL, buf, "Error", 0x10010);
    _exit(0);
    }
    rVal.vt = VT_DISPATCH;
    }
    VariantCopy(&ap p, &rVal);
    VariantClear(&r Val);

    // Line 3: app . visible = 1
    rVal.vt = VT_I4;
    rVal.lVal = 1;
    VariantCopy(&ro ot[++level], &app);
    AutoWrap(DISPAT CH_PROPERTYPUT, NULL, root[level].pdispVal, L"visible", 1, rVal);
    VariantClear(&r oot[level--]);
    VariantClear(&r Val);

    // Line 4: app . workbooks . add
    VariantCopy(&ro ot[++level], &app);
    AutoWrap(DISPAT CH_PROPERTYGET| DISPATCH_METHOD , &root[level+1], root[level++].pdispVal, L"workbooks" , 0);
    AutoWrap(DISPAT CH_METHOD, NULL, root[level].pdispVal, L"add", 0);
    VariantClear(&r oot[level--]);
    VariantClear(&r oot[level--]);

    // Line 5: MsgBox Slow fill example...
    ::MessageBox(NU LL, "Slow fill example...", "MsgBox", MB_SETFOREGROUN D);

    // Line 6: dim i as long
    VARIANT i = {0};

    // Line 7: dim j as long
    VARIANT j = {0};

    // Line 8: with app . activesheet
    VariantCopy(&ro ot[++level], &app);
    AutoWrap(DISPAT CH_PROPERTYGET| DISPATCH_METHOD , &rVal, root[level].pdispVal, L"activeshee t", 0);
    VariantClear(&r oot[level--]);
    VariantCopy(&ro ot[++level], &rVal);
    VariantClear(&r Val);

    // Line 9: for i = 1 to 15
    {
    long endVali = 15;
    i.vt = VT_I4;
    for(i.lVal=1; i.lVal<=endVali ; i.lVal++) {

    // Line 10: for j = 1 to 15
    {
    long endValj = 15;
    j.vt = VT_I4;
    for(j.lVal=1; j.lVal<=endValj ; j.lVal++) {

    // Line 11: . cells i , j . value = i
    VariantCopy(&rV al, &i);
    VariantCopy(&pa rm[0], &i);
    VariantCopy(&pa rm[1], &j);
    AutoWrap(DISPAT CH_PROPERTYGET| DISPATCH_METHOD , &root[level+1], root[level++].pdispVal, L"cells", 2, parm[1], parm[0]);
    VariantClear(&p arm[0]);
    VariantClear(&p arm[1]);
    AutoWrap(DISPAT CH_PROPERTYPUT, NULL, root[level].pdispVal, L"value", 1, rVal);
    VariantClear(&r oot[level--]);
    VariantClear(&r Val);

    // Line 12: DoEvents
    {
    MSG msg;
    while(PeekMessa ge(&msg, NULL, NULL, NULL, PM_REMOVE)) {
    TranslateMessag e(&msg);
    DispatchMessage (&msg);
    }
    }

    // Line 13: next j
    }
    }

    // Line 14: next i
    }
    }

    // Line 15: msgbox Click me to clear range
    ::MessageBox(NU LL, "Click me to clear range", "MsgBox", MB_SETFOREGROUN D);

    // Line 16: . range A1:O15 . Clear
    parm[0].vt = VT_BSTR; parm[0].bstrVal = ::SysAllocStrin g(L"A1:O15");
    AutoWrap(DISPAT CH_PROPERTYGET| DISPATCH_METHOD , &root[level+1], root[level++].pdispVal, L"range", 1, parm[0]);
    VariantClear(&p arm[0]);
    AutoWrap(DISPAT CH_METHOD, NULL, root[level].pdispVal, L"Clear", 0);
    VariantClear(&r oot[level--]);

    // Line 17: end with
    VariantClear(&r oot[level--]);

    // Line 18: msgbox Now the fast way!
    ::MessageBox(NU LL, "Now the fast way!", "MsgBox", MB_SETFOREGROUN D);

    // Line 19: dim arr 1 to 15 , 1 to 15 as long
    VARIANT arr;
    arr.vt = VT_ARRAY | VT_VARIANT;
    {
    SAFEARRAYBOUND sab[2];
    sab[0].lLbound = 1; sab[0].cElements = 15-1+1;
    sab[1].lLbound = 1; sab[1].cElements = 15-1+1;
    arr.parray = SafeArrayCreate (VT_VARIANT, 2, sab);
    }

    // Line 20: for i = 1 to 15
    {
    long endVali = 15;
    i.vt = VT_I4;
    for(i.lVal=1; i.lVal<=endVali ; i.lVal++) {

    // Line 21: for j = 1 to 15
    {
    long endValj = 15;
    j.vt = VT_I4;
    for(j.lVal=1; j.lVal<=endValj ; j.lVal++) {

    // Line 22: arr i , j = i
    VariantCopy(&rV al, &i);
    {
    VARIANT tmp = {0};
    long indices[] = {i.lVal,j.lVal} ;
    VariantCopy(&tm p, &rVal);
    SafeArrayPutEle ment(arr.parray , indices, (void*)&tmp);
    }
    VariantClear(&r Val);

    // Line 23: next j
    }
    }

    // Line 24: next i
    }
    }

    // Line 25: app . activesheet . range A1:O15 . value = arr
    VariantCopy(&rV al, &arr);
    VariantCopy(&ro ot[++level], &app);
    AutoWrap(DISPAT CH_PROPERTYGET| DISPATCH_METHOD , &root[level+1], root[level++].pdispVal, L"activeshee t", 0);
    parm[0].vt = VT_BSTR; parm[0].bstrVal = ::SysAllocStrin g(L"A1:O15");
    AutoWrap(DISPAT CH_PROPERTYGET| DISPATCH_METHOD , &root[level+1], root[level++].pdispVal, L"range", 1, parm[0]);
    VariantClear(&p arm[0]);
    AutoWrap(DISPAT CH_PROPERTYPUT, NULL, root[level].pdispVal, L"value", 1, rVal);
    VariantClear(&r oot[level--]);
    VariantClear(&r oot[level--]);
    VariantClear(&r oot[level--]);
    VariantClear(&r Val);

    // Line 26: msgbox All done.
    ::MessageBox(NU LL, "All done.", "MsgBox", MB_SETFOREGROUN D);

    // Line 27: app . activeworkbook . saved = 1
    rVal.vt = VT_I4;
    rVal.lVal = 1;
    VariantCopy(&ro ot[++level], &app);
    AutoWrap(DISPAT CH_PROPERTYGET| DISPATCH_METHOD , &root[level+1], root[level++].pdispVal, L"activeworkboo k", 0);
    AutoWrap(DISPAT CH_PROPERTYPUT, NULL, root[level].pdispVal, L"saved", 1, rVal);
    VariantClear(&r oot[level--]);
    VariantClear(&r oot[level--]);
    VariantClear(&r Val);

    // Line 28: app . quit
    VariantCopy(&ro ot[++level], &app);
    AutoWrap(DISPAT CH_METHOD, NULL, root[level].pdispVal, L"quit", 0);
    VariantClear(&r oot[level--]);

    // Line 29: set app = nothing
    VariantClear(&a pp);

    // Clearing variables
    VariantClear(&a pp);
    VariantClear(&i );
    VariantClear(&j );
    VariantClear(&a rr);

    // Close the OLE Library...
    OleUninitialize ();[/code]
    Last edited by sicarie; Aug 20 '07, 12:55 PM. Reason: Code tags are on the right when creating or replying to a thead. They want to be used. They like to be used.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    That's a lot of interesting code there.

    Did you have a question about it? Is it creating an error? What's the relevance of the code to your dll issue? Have you searched how to make a dll in VC++?

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Well start by creating a DLL project in MSVC (VC++). This will create the basic file structure and compile and link options you need.

      Then add a function with your code in it including the additional function type modifiers require for a DLL function (defined in the symbol DLL_EXPORT I believe).

      Comment

      Working...