What is error in my code?

S

sjlee

Hello,

Following code is a fragment of the ClipShare
(http://www.codeproject.com/dotnet/clipsend.asp)

other download site => http://www.serlio.com/resources/ClipShare.aspx


When I copy english html text in IE and then paste it to ms word,
it is pasted correctly. But when I copy KOREAN html text in IE and
then paste it to ms word or other ms office tools, it is crashed.

this code just get data in clipboard, and put it into clipboard again.

After painful investigation, I think it may be a bug in .net framework.

What do you think about my problem?

FYI, while english character can be represented in 1-byte(1 UTF-8 encoded
char),
korean character can be represented in 2-byte(2 UTF-8 encoded chars).

Thank you in advance.

Regards,
SJLEE

====
ArrayList dataObjects = new ArrayList();
IDataObject clipboardData = Clipboard.GetDataObject();
string[] formats = clipboardData.GetFormats();
for (int i=0; i < formats.Length; i++)
{
object clipboardItem = clipboardData.GetData(formats);
if (clipboardItem != null && clipboardItem.GetType().IsSerializable)
{
Console.WriteLine("sending {0}", formats);
// for each item, save the format string followed by the clipboard
data
dataObjects.Add(formats);
dataObjects.Add(clipboardItem);
}
else
Console.WriteLine("ignoring {0}", formats);
}

// Send the clipboard data in the array list if there is any
if (dataObjects.Count > 0)
{
Cursor.Current = Cursors.WaitCursor;
AddToClip(dataObjects);
Cursor.Current = Cursors.Default;
}

....

public void AddToClip(ArrayList theData)
{
try
{
DataObject dataObj = new DataObject();
for (int i = 0; i < theData.Count; i++)
{
string format = (string)theData[i++];
dataObj.SetData(format, theData);
}

Clipboard.SetDataObject(dataObj, true);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
 
S

sjlee

I tested using customized win32 code to get html data from clipboard
and then put it to clipboard again.
It has no problem.

Finally I conclude that .net framework HTML data getting from or setting in
clipboard has a bug when it does non-english character's utf-8 en/decoding.

Also for ms office tool same problems occur when non-english text is copied
or pasted.

I test it in .net 1.1 and 2.0.

How can I report the bug to microsoft?

Thank you in advance.

Regards,
SJLEE

===
win32 code

__declspec(dllexport) DWORD GetHtmlClipboardData(LPSTR ReadHtmlData)
{
HANDLE hMem;
LPSTR lpData;
int nClipSize;

if (OpenClipboard(NULL))
{
hMem = GetClipboardData(RegisterClipboardFormat("HTML Format"));
if (hMem != NULL)
{
lpData = (LPSTR)GlobalLock(hMem);
if (lpData != NULL)
{
nClipSize = strlen(lpData);

MessageBox(NULL, lpData, "hahahihi", MB_OK);

CopyMemory(ReadHtmlData, lpData, nClipSize);

GlobalUnlock(hMem);
CloseClipboard();
return 0;
}
else
{
CloseClipboard();
return 3;
}
}
else
{
CloseClipboard();
return 2;
}
}

return 1;
}

__declspec(dllexport) DWORD SetHtmlClipboardData(LPSTR HtmlData)
{
HANDLE hMem;
LPSTR lpData;
int nClipSize;

if (OpenClipboard(NULL))
{
EmptyClipboard();
nClipSize = strlen(HtmlData);
hMem = GlobalAlloc(GMEM_MOVEABLE, nClipSize);
if (hMem != NULL)
{
lpData = (LPSTR) GlobalLock(hMem);
memcpy(lpData, HtmlData, nClipSize);
MessageBox(NULL, lpData, "hahahihi", MB_OK);
GlobalUnlock(hMem);

SetClipboardData(RegisterClipboardFormat("HTML Format"), hMem);
CloseClipboard();
return 0;
}
else
{
CloseClipboard();
return 2;
}
}

return 1;
}

===
..net c# code

if (htmlDataAvail && htmlPrvDataAvail == false)
{
int htmlDataSize = ((string)clipboardHtmlItem).Length;
byte[] htmlData = new byte[htmlDataSize];

if (GetHtmlClipboardData(htmlData))
MessageBox.Show(this, "this", "that");
if (SetHtmlClipboardData(htmlData))
MessageBox.Show(this, "THIS", "THAT");
htmlPrvDataAvail = true;
}
 

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