Getting Clipboard Handle using GetClipboardData in C#

R

Roohi

hi

I am trying to copy an image to a clipboard and then get a
handle to the clipboard using API calls GetClipboardData().

Following is the snippet of our code. Could any one please
point out what am I missing here since I never get the
handle to the object on clipboard

<snippet>

OpenClipboard(0);

DataObject m_data = new DataObject();

m_data.SetData(DataFormats.Dib,imgCardImage.Image);

Clipboard.SetDataObject(m_data,false);

hDib = GetClipboardData(CF_DIB); // CF_DIB = 8;

</snippet>


hDib returned is always 0 where it actually had to return
a handle to the clipboard;

Thanks in Advance.

~Roohi
 
M

Morten Wennevik

Not familiar with GetClipboardData.
Why do you need a handle to it instead of using .NET's
ClipBoard.GetDataObject to retrieve it?
 
R

Roohi

I need to pass this handle to some other API which accepts
handle to the clipboard as a parameter

Hence I need it

Thanks
 
M

Morten Wennevik

Hm, maybe Clipboard.SetDataObject() closes the clipboard you opened with
OpenClipboard().
Try moving OpenClipBoard(0); after SetDataObject()
 
J

Jeff Gaines

hi

I am trying to copy an image to a clipboard and then get a
handle to the clipboard using API calls GetClipboardData().

~Roohi

Roohi

Can you modify this to do what you need:

public static bool JCopyFilesToClipboard(string[] strFiles, POINT pt)
{
bool blnReturn = false;
byte[] bData;
DROPFILES df = new DROPFILES();
int intChar, intFile, intDataLen, intPos;
IntPtr ipGlobal = IntPtr.Zero;

// Calculate total data length
intDataLen = 0;
for(intFile = 0; intFile <= strFiles.GetUpperBound(0);
intFile++)
intDataLen += strFiles[intFile].Length + 1;

// Terminating double zero
intDataLen++;

bData = new Byte[intDataLen];
intPos = 0;

// Build null terminated list of files
for(intFile = 0; intFile <= strFiles.GetUpperBound(0);
intFile++)
{
for(intChar = 0; intChar < strFiles[intFile].Length;
intChar++)
{
bData[intPos++] = (byte)strFiles[intFile][intChar];
}
bData[intPos++] = 0;
}
// Terminating double zero
bData[intPos++] = 0;

// Allocate and get pointer to global memory
int intTotalLen = Marshal.SizeOf(df) + intDataLen;
ipGlobal = Marshal.AllocHGlobal(intTotalLen);

if(ipGlobal == IntPtr.Zero)
return false;

// Build DROPFILES structure in global memory.
df.pFiles = Marshal.SizeOf(df);
df.pt = pt;
df.fNC = false;
df.fWide = 0;
Marshal.StructureToPtr(df, ipGlobal, true);
IntPtr ipNew = new IntPtr(ipGlobal.ToInt32() +
Marshal.SizeOf(df));
Marshal.Copy(bData, 0, ipNew, intDataLen);

// Open and empty clipboard
if(!OpenClipboard(IntPtr.Zero))
return false;

EmptyClipboard();

// Copy data to clipboard
blnReturn = (SetClipboardData(CF_HDROP, ipGlobal) !=
IntPtr.Zero);
if(!blnReturn)
Marshal.FreeHGlobal(ipGlobal);

// Clean up
CloseClipboard();
// Clipboard responsible for freeing memory
// Marshal.FreeHGlobal(ipGlobal);

return blnReturn;
}

It's a different file format but the principle is similar.
 
J

Jeff Gaines

hi

I am trying to copy an image to a clipboard and then get a
handle to the clipboard using API calls GetClipboardData().

Following is the snippet of our code. Could any one please
point out what am I missing here since I never get the
handle to the object on clipboard


hDib returned is always 0 where it actually had to return
a handle to the clipboard;

Thanks in Advance.

~Roohi

Roohi

I am not sure now if my earlier post answers your question :-(

I have tried what you are doing - you must call open clipboard before
trying to get the handle to the data. Is the clipboard still open when
you make this call or have you already closed it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top