LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

WINOLEAPI doDragDrop How to?

Hi

 

Im trying to do a drag and drop of an image from my app to another app. I want to do it the same as windows explorer does it.

I came a cross the WINOLEAPI doDragDrop() as its seem promising but all the examples and code are written C++ or C# so just cant get it to work with Labwindows CVI 2013.

 

Can some one please assist?

 

 

Help share your knowlegde
0 Kudos
Message 1 of 8
(5,211 Views)

Hello,

 

What kind of issues are you running into? Are you having trouble accessing the functions, themselves? If so, you should be able to access them. The following help documents talk about which versions of CVI have which functions as well as how to enable them.

 

Availability of Interface to Application Programmatic Interface (API) Functions

http://zone.ni.com/reference/en-XX/help/370051M-01/cvi/programmerref/availability_win32_functions/

 

Calling Functions from the Interface to Win32 Application Programmatic Interface (API) in LabWindows/CVI

http://zone.ni.com/reference/en-XX/help/370051M-01/cvi/programmerref/calling_windows_sdk_functions/

 

If you are running into problems with actual usage of the function, then CVI should have definitions and help documents for the functions themselves to help you get started. The general architecture should be similar between CVI and the c++ languages, it is just going to be a change in syntax. 

 

Long story short, exactly what issue are you running into when trying to use the function?

 

Best,

 

Shamik C

Applications Engineer 

National Instruments 

http://www.ni.com/support 

0 Kudos
Message 2 of 8
(5,186 Views)

Hi ShamWOW thanks for the reply.

I am not sure if im doing this right but CVI 2013 just says FATAL Error when i try to run doDragDrop.

I edited an example that i found on the internet so that it will compile with CVI.

Im not sure what i am doing wrong or forgetting to do, as i followed the MSDN documentation.

#include "ShlObj.h"

void StartDragDropFile(char *FilePathName,LRESULT* pResult) 
{
	
//COleDataSource datasrc;
IDataObject pdo;
IDropSource pds;	

HGLOBAL        hgDrop;
DROPFILES*     pDrop;
int            nSelItem;
UINT           uBuffSize = 0;
TCHAR*         pszBuff;
UINT g_uCustomClipbrdFormat = RegisterClipboardFormat ( "DisplayGenerator_3BCFE9D1_6D61_4cb6_9D0B_3BB3F643CA82");
FORMATETC      etc = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_FILE };

    *pResult = 0;   // return value ignored

	LPVOID pvReserved;
	OleInitialize(pvReserved); 
    // For every selected item in the list, put the filename into lsDraggedFiles.



    // Calculate the # of chars required to hold this string.

    uBuffSize += lstrlen ( FilePathName ) + 1;

    // Add 1 extra for the final null char, and the size of the DROPFILES struct.

    uBuffSize = sizeof(DROPFILES) + sizeof(TCHAR) * (uBuffSize + 1);

    // Allocate memory from the heap for the DROPFILES struct.

    hgDrop = GlobalAlloc ( GHND | GMEM_SHARE, uBuffSize );

    if ( NULL == hgDrop )
        return;

    pDrop = (DROPFILES*) GlobalLock ( hgDrop );

    if ( NULL == pDrop )
        {
        GlobalFree ( hgDrop );
        return;
        }

    // Fill in the DROPFILES struct.

    pDrop->pFiles = sizeof(DROPFILES);

#ifdef _UNICODE
    // If we're compiling for Unicode, set the Unicode flag in the struct to
    // indicate it contains Unicode strings.

    pDrop->fWide = TRUE;
#endif

    // Copy all the filenames into memory after the end of the DROPFILES struct.

    pszBuff = (TCHAR*) ((pDrop) + sizeof(DROPFILES));

	lstrcpy ( pszBuff, (LPCTSTR) FilePathName );
    //pszBuff = 1 + _tcschr ( pszBuff, '\0' );
	pszBuff = 1 +  ( pszBuff, '\0' );
    GlobalUnlock ( hgDrop );

    // Put the data in the data source.

    //datasrc.CacheGlobalData ( CF_HDROP, hgDrop, &etc );

    // Add in our own custom data, so we know that the drag originated from our 
    // window.  CMyDropTarget::DragEnter() checks for this custom format, and
    // doesn't allow the drop if it's present.  This is how we prevent the user
    // from dragging and then dropping in our own window.
    // The data will just be a dummy bool.
HGLOBAL hgBool;

    hgBool = GlobalAlloc ( GHND | GMEM_SHARE, sizeof(char) );

    if ( NULL == hgBool )
        {
        GlobalFree ( hgDrop );
        return;
        }

    // Put the data in the data source.

    etc.cfFormat = g_uCustomClipbrdFormat;
    
    //datasrc.CacheGlobalData ( g_uCustomClipbrdFormat, hgBool, &etc );


    // Start the drag 'n' drop!

//DROPEFFECT dwEffect = datasrc.DoDragDrop ( DROPEFFECT_COPY | DROPEFFECT_MOVE );
	
	STGMEDIUM stgmed = {TYMED_FILE,{FilePathName},0};
	pdo.lpVtbl->SetData(&pdo,&etc,&stgmed,0);
	DWORD dwEffect;
	HRESULT hr = DoDragDrop (&pdo, &pds,DROPEFFECT_MOVE | DROPEFFECT_COPY, &dwEffect);

    // If the DnD completed OK, we remove all of the dragged items from our
    // list.

    switch ( dwEffect )
        {
        case DROPEFFECT_COPY:
        case DROPEFFECT_MOVE:
            {
            // The files were copied or moved.
            // Note: Don't call GlobalFree() because the data will be freed by the drop target.

            }
        break;

        case DROPEFFECT_NONE:
            {
            // This needs special handling, because on NT, DROPEFFECT_NONE
            // is returned for move operations, instead of DROPEFFECT_MOVE.
            // See Q182219 for the details.
            // So if we're on NT, we check each selected item, and if the
            // file no longer exists, it was moved successfully and we can
            // remove it from the list.

			GlobalFree ( hgDrop );
                    GlobalFree ( hgBool );
            }
        break;  // end case DROPEFFECT_NONE
        }   // end switch
	OleUninitialize();
}
Help share your knowlegde
0 Kudos
Message 3 of 8
(5,116 Views)

 

I cannot see anything obvious that would cause the Fatal error. However, do you know exactly where the fatal error happens at runtime? Does it happen at the DoDragDrop command, or somewhere else? I recommend setting some breakpoints to find out where the issue is located.

 

 

0 Kudos
Message 4 of 8
(5,084 Views)

I have done that and the error is when the DoDragDrop command is called.

Were you able to use this code?

Help share your knowlegde
0 Kudos
Message 5 of 8
(5,074 Views)

Hello Shako,

 

I couldn't run your code because it contains only one function I don't know where it should be called from.

Anyways when I compile the code in CVI 2015 I get some warnings

 dd.c - 7 warnings
  66, 13    warning: incompatible integer to pointer conversion assigning to 'TCHAR *' (aka 'char *') from 'int' 
  90, 20    warning: implicit conversion loses integer precision: 'UINT' (aka 'unsigned int') to 'CLIPFORMAT' (aka 'unsigned short') 
  99, 37    warning: incompatible pointer types initializing 'HBITMAP' (aka 'struct HBITMAP__ *') with an expression of type 'char *' 
  102, 13    warning: unused variable 'hr' 
  12, 16    warning: unused variable 'nSelItem' 
  100, 5    warning: variable 'pdo' is uninitialized when used here 
      7, 1    note: variable 'pdo' is declared here
  21, 19    warning: variable 'pvReserved' is uninitialized when used here 
      20, 22    note: initialize the variable 'pvReserved' to silence this warning

Some of these warnings might reveal problems and if you fix them you might be closer to a working solution.

0 Kudos
Message 6 of 8
(4,953 Views)

Hi

 

Thanks for replying.

I still wasnt able to get it working. Changed some of the varibles as per the warnings but did not help.

If you get it working please let me know or if you can point me in a another direction to solve this problem.

Help share your knowlegde
0 Kudos
Message 7 of 8
(4,886 Views)

Hi Shako,

 

I'm wondering how you're calling this function in the larger context of your code. Where is the entrance point? Are you calling this from a Main? I tried to reproduce the error, and I see similar warnings. However, there seem to be issues with some of the data types you're using - particularly  pszBuff = (TCHAR*), pDrop->pFiles = sizeof(DROPFILES);  and the parameters being accepted by this prototype (StartDragDropFiles).

 

Can you post the entirety of your code here so we can try and reproduce / debug more thoroughly? Or, can you link the example from MSDN that you modified so we can from that starting point?

 

Best,

Austin H.
Chief Product Manager
Test Software
Message 8 of 8
(4,867 Views)