Clipboard.GetDataObject();

  • Thread starter Thread starter Max2006
  • Start date Start date
M

Max2006

Hi,

No matter what I have in clipboard Clipboard.GetDataObject(); returns null.
I suspect it might be a problem with Vista or VS2008 IDE debug mode problem?

Is there any way to track why the following statement is always null?

IDataObject iData = Clipboard.GetDataObject();

Thank you,
Max
 
Hi Max,

Where do you run this code snippet? Have you tried to run your application
as elevated in Vista? I suspect this issue is caused by the security
enhancement of Vista.

Can you provide detailed steps for me to reproduce it locally? Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Jeffry,

Here is the steps to reproduce the problem:

1) use visual studio 2008 team system on windows vista sp1
2) Run the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AdjustClipboard
{
class Program
{
static void Main(string[] args)
{
IDataObject iData = Clipboard.GetDataObject();

string s = Clipboard.GetData(DataFormats.Text).ToString();
}
}
}


3) you will get null exception!



Thank you for help,
Max
 
The documentation on MSDN says you need the [STAThreadAttribute()] on main
to use the call to GetDataObject(). With that change, your code worked fine
for me on Vista (Home Edition).


Max2006 said:
Hi Jeffry,

Here is the steps to reproduce the problem:

1) use visual studio 2008 team system on windows vista sp1
2) Run the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AdjustClipboard
{
class Program
{
static void Main(string[] args)
{
IDataObject iData = Clipboard.GetDataObject();

string s = Clipboard.GetData(DataFormats.Text).ToString();
}
}
}


3) you will get null exception!



Thank you for help,
Max



"Jeffrey Tan[MSFT]" said:
Hi Max,

Where do you run this code snippet? Have you tried to run your
application
as elevated in Vista? I suspect this issue is caused by the security
enhancement of Vista.

Can you provide detailed steps for me to reproduce it locally? Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you.
Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent
issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each
follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
Hi Max,

Thanks for your feedback, yes, I can reproduce this behavior.

I think "Family Tree Mike" has kindly pointed out the root cause. We need
STAThreadAttribute[] on the Main method, or an exception will throw. This
requirement is documented in the MSDN link below:
http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.getda
taobject.aspx

Once you added STAThreadAttribute, it should work well. I tested this on my
Vista Enterprise SP1 machine, it works well. Below is the test code:

[STAThread]
static void Main()
{
IDataObject iData = Clipboard.GetDataObject();
string s = Clipboard.GetData(DataFormats.Text).ToString();
MessageBox.Show(s);
}
Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
WOW, That was a neat point. Thank you very much for help

Family Tree Mike said:
The documentation on MSDN says you need the [STAThreadAttribute()] on main
to use the call to GetDataObject(). With that change, your code worked
fine for me on Vista (Home Edition).


Max2006 said:
Hi Jeffry,

Here is the steps to reproduce the problem:

1) use visual studio 2008 team system on windows vista sp1
2) Run the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AdjustClipboard
{
class Program
{
static void Main(string[] args)
{
IDataObject iData = Clipboard.GetDataObject();

string s = Clipboard.GetData(DataFormats.Text).ToString();
}
}
}


3) you will get null exception!



Thank you for help,
Max



"Jeffrey Tan[MSFT]" said:
Hi Max,

Where do you run this code snippet? Have you tried to run your
application
as elevated in Vista? I suspect this issue is caused by the security
enhancement of Vista.

Can you provide detailed steps for me to reproduce it locally? Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments
and
suggestions about how we can improve the support we provide to you.
Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent
issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each
follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach
the
most efficient resolution. The offering is not appropriate for
situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are
best
handled working with a dedicated Microsoft Support Engineer by
contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
Back
Top