PDA

View Full Version : clipboard problem



srinirocks
08-19-2009, 01:37 PM
I am using the following code to save mappiont map as an image. When I run my application as a service, there is problem. Is there any other way to get the map as an image??

The Problem: (I got this somewhere online.)
Only STA threads can access the clipboard, Windows Services threads enter
the MTA by default. Even if you run your clipboard stuff on a new thread
initialized to enter an STA, you will encounter following issues:
1) STA threads must create a Window and pump it's message queue.
2) The clipboard is a shared memory section, mapped in all the processes
memory space running in the same WindowsStation\Desktop pair sharing the
same Session. Windows Services and Interactive applications run in distinct
pairs (unless you set "Access Interactive Desktop", which is a bad idea),
so, by default, they don't share the clipboard.
On Vista ( and on systems where FUS is enabled/used) it's no longer
possible to use the clipboard to pass data across the session boundaries.
Services and user applications do no longer share the same session on Vista.


//--code
// save current clipboard
object save = Clipboard.GetDataObject();

// copy map to clipboard
MyMap.CopyMap();

// get picture from clipboard
IDataObject pict = Clipboard.GetDataObject();

// check available convert formats and convert
string[] formats = pict.GetFormats();
foreach ( string s in formats )
if ( s.EndsWith(System.Windows.Forms.DataFormats.Bitmap ) ) {
img = (System.Drawing.Image)pict.GetData(System.Windows. Forms.DataFormats.Bitmap);
break;
}
//try to restore clipboard
Clipboard.SetDataObject(save);




Srini

Wilfried
08-20-2009, 03:37 AM
Hi,

If I understand you right you want to pass data trough the clipboard from a service to other applications. You have several possibilities:

- if you have written the target application(s) also then you can pass the data from your service trough many other methods. This can be a 'named pipe', a 'tcp session', a file, and other things.

- if you really do want the data available on the clipboard then you have to write a little application that runs in background, that received the data from your service (see previous paragraph) and copy it on the clipboard. This little application can start whenever a user logs in.

srinirocks
08-20-2009, 06:20 PM
Thank you Wilfried for your reply.

I am sorry I coudn't explain my problem properly.

My target is to save the map view as an image. GetPictureFromObject() method doesn't seem to work in CSharp. So I think most of them are using Map.CopyMap() method instead. CopyMap() method is copying the map to Clipboard. But I found that Windows service cannot access the Clipboard. So CopyMap() method doesnt' seem to my best bet. I should find some other way to save map as an image. Any Ideas?

If anybody has success with GetPictureFromObject() in C#, please let me know.


Srini

Wilfried
08-21-2009, 02:24 AM
Hi,

According to the help GetPictureFromObject returns a "Visual Basic picture object". Whatever it is I assume that it is something only for the eyes of VB :)

Maybe the most simple thing is to save the map to a file? Then you can read it again by a foreground application.