Setting my Display Resolution - c#

I'm a C# developer and I have to change my display resolution regularly.
There are plenty of examples on how to read the current display resolutions:
SystemInformation.PrimaryMonitorSize
I found the ChangeDisplaySettingsEx Function
Is the only way to do this in C# is with PInvoke???
It seems odd to me that it is very easy to get this information out, but difficult to set it...

You'll have to make a PInvoke call to ChangedisplaySetting.
Here's a link that has some sample code, http://www.xtremedotnettalk.com/printthread.php?t=73184.

I also recommend that you check out Jared Parsons PInvoke Toolkit. You can download it here:
http://www.codeplex.com/clrinterop/Release/ProjectReleases.aspx?ReleaseId=14120
It makes adding pinvoke signatures to your code a breeze. It doesn't always pick the right interop types, but it's usually pretty close, and even if you have to make some changes its still usually quicker than translating everything by hand.

Simple answer: Yes.
You should use PInvoke.

Related

Sending control keys with DDeltaSolution UIDeskAutomationSpy

I have been using DDeltaSolution's UIDeskAutomationSpy to enhance some of my Coded UI testing, initially based on the MS Code UI test (cuit) framework.
However there is very limited documentation and even after using dotNetPeek to inspect the internals of the UIDeskAutomationSpy exe and associated dll, I can't see how to send control keys (Shift/Control/Alt) to a component.
There are two relevant methods
SendKeys()
SimulateSendKeys()
but both just take a string as input.
I've even got as far as thinking about trying to use Cecil to try and modify the binaries (is this possible?), but this is a desperate measure. Does anyone know any better, or know of any better documentation?
This is a surprisingly powerful tool, but no-one seems to have heard about it.
I'm not positive, but if I were you I'd try using the list of control key strings found HERE since automation spy is based on .NET. Let me know if it works!

AutoIt - Where is the HotKeySet method?

We are using AutoItX3.dll
Auto It's documentation states there is a HotKeySet method which would probably be the solution to a particularly painful problem we are experiencing.
http://www.autoitscript.com/autoit3/docs/functions/HotKeySet.htm
When I look at the available methods within the .dll, however, there is nothing related to HotKeySet.
Anyone recommend where to find this method?
HotkeySet uses a callback to a user defined function, converting the method for use in other languages isn't really worth the effort.
Instead you will have to call the WinAPI directly. Google search for RegisterHotkey C# wpf gets a lot of results. The first result I've used before.

Hook into the Windows File Copy API from C#

Is there a way to hook into the Windows File Copy API from C#? I'm aware this would require unmanaged code, but a code sample or starter would be helpful. I've already seen the C++ code, but it's all greek.
UPDATE: I apologize, I should have been more clear about my intentions. I wish to actually change the copy feature of Windows to be more rigid (e.g. allow queing, scheduling, handle restarts, pauses, etc.). When I said hook, I meant API hook so that when someone starts a copy I get the sources and destinations and can handle it to my heart's desire. I'm old school and used to hook the Mac OS API a lot to do these things so I assumed that in the C++ WINAPI world there was some type of equiv.
Update:
As others have stated, why not just use System.IO.File.Copy(...)? It calls this same underlying API. As Michael G points out, perhaps you intend to call the the FileCopyEx API that allows you to hook progress-indication callbacks(???) That's really the only reason to P/Invoke file-copy stuff in .NET. Details on how to implement FileCopyEx that can be found here: http://pinvoke.net/default.aspx/kernel32/CopyFileEx.html
Original answer: (which you really shouldn't use...)
Code snippet removed because you really shouldn't use it...
If you're hell-bent on making busted-code, you can find out how to use it at: Found at http://pinvoke.net/default.aspx/kernel32/CopyFile.html
I wish to actually change the copy feature of Windows to be more rigid
You shouldn't do that in managed code, because of the same reasons you should not write managed shell extensions.
You can do so by calling System.IO.File.Copy. Its internal implementation already uses the Windows API.
Edit: File.Copy also handles permissions correctly and has the benefit of throwing an exception with meaningful data if something fails, so you don't have to manually check and analyze the return status.
You can use Deviare API Hook that lets you intercept any API from .NET and read parameters using VARIANT types. There is a full example very easy to follow in C#.
The other benefit of using unmanaged Copy File API is the ability to have a progress callback.
Note: as stated in other answers, I would use the managed version of File.Copy as it's safer, and can usually do everything you require.

How to turn off *multi display* in c#?

There are many articles about "how to turn off monitors in c#", but they all turn off all the monitors. Is there any possible way to turn off a particular monitor?
A little bit of research lead me here:
http://www.codeguru.com/forum/showthread.php?p=1011720#post1011720
Do you know how to call a function like that in c#?
-- Edit
Looks like this guy has done it all for you:
http://www.codeproject.com/KB/dotnet/Display_Settings.aspx
For calling Win32 Functions not exposed in the .NET Framework
PInvoke.net
is a good ressource.
The function mentioned in the link of silky can be found here.
Also have a look at the used data types.
Just copy the code mentioned in "C# Signature" to your project.
Given that the monitor belongs to the user, not you, why do you want to turn it off.
This is just the sort of think I think a OS should protect me from when a application tries it, so expect problems…

How to make a "Call stack diagram"

Creating a call stack diagram
We have just recently been thrown into a big project that requires us to get into the code (duh).
We are using different methods to get acquainted with it, breakpoints etc. However we found that one method is to make a call tree of the application, what is the easiest /fastest way to do this?
By code? Plugins? Manually?
The project is a C# Windows application.
With the static analyzer NDepend, you can obtain a static method call graph, like the one below. Disclaimer: I am one of the developers of the tool
For that you just need to export to the graph the result of a CQLinq code query:
Such a code query, can be generated actually for any method, thanks to the right-click menu illustrated below.
Whenever I start a new job (which is frequently as I am a contractor) I spend two to three days reading through every single source file in the repository, and keep notes against each class in a simple text file. It is quite laborious but it means that you get a really good idea how the project fits together and you have a trusty map when you need to find the class that does somethnig.
Altought I love UML/diagramming when starting a project I, personally, do not find them at all useful when examining existing code.
Not a direct answer to your question, but NDepend is a good tool to get a 100ft view of a codebase, and it enables you to drill down into the relationships between classes (and many other features)
Edit: I believe the Microsoft's CLR Profiler is capable of displaying a call tree for a running application. If that is not sufficient I have left the link I posted below in case you would like to start on a custom solution.
Here is a CodeProject article that might point you in the right direction:
The download offered here is a Visual
Studio 2008 C# project for a simple
utility to list user function call
trees in C# code.
This call tree lister seems to work OK
for my style of coding, but will
likely be unreliable for some other
styles of coding. It is offered here
with two thoughts: first, some
programmers may find it useful as is;
second, I would be appreciative if
someone who is up-to-speed on C#
parsing would upgrade it by
incorporating an accurate C# parser
and turn out an improved utility that
is reliable regardless of coding style
The source code is available for download - perhaps you can use this as a starting point for a custom solution.
You mean something like this: http://erik.doernenburg.com/2008/09/call-graph-visualisation-with-aspectj-and-dot/
Not to be a stuck record, but if I get it running and pause it a few times, and each time capture the call stack, that gives me a real good picture of the call structure that accounts for the most time. It doesn't give me the call structure for things that happen real fast, however.

Categories