Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test {
class Program {
static void Main(string[] args) {
for (int i = 0; i < 1000000000; i++) {
WindowHandler.testOverlay();
}
}
}
}
WindowHandler.cs:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test {
static class WindowHandler {
private const String WINDOW_TITLE = "Minesweeper";
private static IntPtr windowHandle = IntPtr.Zero;
public static void testOverlay() {
if (windowHandle == IntPtr.Zero) {
windowHandle = getWindowHandle();
}
Graphics g = Graphics.FromHwnd(windowHandle);
g.FillRectangle(new SolidBrush(Color.White), 0, 0, 10000, 10000);
}
private static IntPtr getWindowHandle() {
foreach (Process proc in Process.GetProcesses()) {
if (proc.MainWindowTitle == WINDOW_TITLE) {
return proc.MainWindowHandle;
}
}
MessageBox.Show("Error: Unable to find window.");
return IntPtr.Zero;
}
}
}
I am not sure what I am doing wrong. I am writing a Minesweeper Solver and I am trying to overlay graphics on the Minesweeper window to provide debugging information. Unfortunately, it doesn't seem to work at all as I do not see any change on my screen. I am looping 1000000000 times in Program.cs just in case it erases my overlay on every frame refresh. I prefer to not try to hook DirectX.
Related
I have a DLL that I have developed. I use this DLL in a website using DllImport.
When I run the website via Visual Studio all is okay, but when I run it with the IIS it is stuck with no errors.
Here's my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Runtime.InteropServices;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
[DllImport("D:\\WebApplication1\\WebApplication1\\bin\\dapreporter.dll", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr fndapreporter(string inifile, int nReport, int nYear, int nMonth, int nDay, int nType, int nCode, int precision);
protected void Page_Load(object sender, EventArgs e)
{
try
{
string iniFile = File.ReadAllText(#"D:\WebApplication1\WebApplication1\bin\WinDAP.ini");
IntPtr pVariant = fndapreporter(iniFile, 0, 2021, 9, 12, 0, 197, 0);
object sHtml = Marshal.GetObjectForNativeVariant(pVariant);
Marshal.FreeCoTaskMem(pVariant);
pVariant = IntPtr.Zero;
Response.Write(sHtml.ToString());
}
catch(Exception ex)
{
Response.Write("ERROR: " + ex.Message);
}
}
}
}
A website's files/folders are relative to the home directory for that particular website. Due to the way DllImport works, one can add the desired directory to the PATH environment variable and then specify only the filename.
Try the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Hosting;
using System.Runtime.InteropServices;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
[DllImport("dapreporter.dll", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr fndapreporter(string inifile, int nReport, int nYear, int nMonth, int nDay, int nType, int nCode, int precision);
protected void Page_Load(object sender, EventArgs e)
{
IntPtr pVariant = IntPtr.Zero;
try
{
//get path to bin directory
//string binDir = Server.MapPath("~/bin/");
string binDir = HostingEnvironment.MapPath("~/bin/");
//add bin directory to PATH so DLLImport can find the DLL
Environment.SetEnvironmentVariable("PATH", String.Format("{0};{1}", Environment.GetEnvironmentVariable("PATH"), binDir));
string iniFile = File.ReadAllText(Path.Combine(binDir, "WinDAP.ini"));
pVariant = fndapreporter(iniFile, 0, 2021, 9, 12, 0, 197, 0);
object sHtml = Marshal.GetObjectForNativeVariant(pVariant);
Response.Write(sHtml.ToString());
}
catch (Exception ex)
{
Response.Write("ERROR: " + ex.Message);
}
finally
{
if (pVariant != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(pVariant);
pVariant = IntPtr.Zero;
}
}
}
}
}
Resources:
Call function from DLL with non-static path
HttpServerUtility.MapPath(String) Method
HostingEnvironment.MapPath(String) Method
httpcontext.current.server.mappath Object reference not set to an instance of an object
I need to set the background of a Windows form to the user's current Desktop wallpaper. How do I do this in C#?
Thanks
You can try the following code. I've tested this code on windows 8 and it works for me:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StringFormatting
{
public partial class WallpaperTest : Form
{
private const UInt32 SPI_GETDESKWALLPAPER = 0x73;
private const int MAX_PATH = 260;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo(UInt32 uAction, int uParam, string lpvParam, int fuWinIni);
public WallpaperTest()
{
InitializeComponent();
this.BackgroundImage = GetCurrentDesktopWallpaper();
this.BackgroundImageLayout = ImageLayout.Stretch;
}
public Image GetCurrentDesktopWallpaper()
{
string currentWallpaper = new string('\0', MAX_PATH);
SystemParametersInfo(SPI_GETDESKWALLPAPER, currentWallpaper.Length, currentWallpaper, 0);
string imageAddress = currentWallpaper.Substring(0, currentWallpaper.IndexOf('\0'));
return Image.FromFile(imageAddress);
}
}
}
Where the code initially came from
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Audio.OpenAL;
namespace OpenTK_OpenAL
{
class Program
{
static unsafe void Main(string[] args)
{
IntPtr device = Alc.OpenDevice(null);
ContextHandle context = Alc.CreateContext(device, (int*)null);
Alc.MakeContextCurrent(context);
string version = AL.Get(ALGetString.Version);
string vendor = AL.Get(ALGetString.Vendor);
string renderer = AL.Get(ALGetString.Renderer);
Console.WriteLine(version);
Console.WriteLine(vendor);
Console.WriteLine(renderer);
Console.ReadKey();
}
}
}
I am looking at the link above and wrote a program, but the program does not work. Can I get a suggestion of why this doesn't work? There is an exception at the very beginning of the code, IntPtr device = Alc.OpenDevice(null);.
I'm trying to capture a Screensaver event, however when I run my application it throws the following exception;
Unable to find an entry point named 'SystemParametersinfo' in DLL 'user32.dll'.
this is my code so far, any and all help would be greatly appreciated =D
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NLog;
using Topshelf;
using OsWatch;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace NotifyIcon
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersinfo(int uAction, int uParam, ref int ipvParam, int fuWinini);
const int SPI_GETSCREENSAVERRUNNING = 114;
static int screenSaverRunning = -1;
int ok = SystemParametersinfo(SPI_GETSCREENSAVERRUNNING, 0, ref screenSaverRunning, 0);
private void ScreenSaver()
{
if (ok == 0)
{
Logger.Trace("SCREENSAVER OFF");
}
if (screenSaverRunning != 0)
{
Logger.Trace("SCREENSAVER ON");
}
}
I am experimenting and trying to figure out DWM and Windows Aero. So far, I think I pretty much have it, all except for some strange reason, when I click the thicker part of my form, my click goes right through it, and I can't figure out how to stop it.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int en;
public Form1()
{
InitializeComponent();
DwmIsCompositionEnabled(ref en);
if (en > 0 && System.Environment.OSVersion.Version.Major >= 6)
{
en = 0;
MARGINS mg = new MARGINS();
mg.m_Buttom = 0;
mg.m_Left = 0;
mg.m_Right = 0;
mg.m_Top = 25;
DwmExtendFrameIntoClientArea(this.Handle, ref mg);
this.BackColor = Color.Magenta;
this.TransparencyKey = Color.Magenta;
}
}
public struct MARGINS
{
public int m_Left;
public int m_Right;
public int m_Top;
public int m_Buttom;
}
[System.Runtime.InteropServices.DllImport("dwmapi.dll")]
public extern static int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margin);
[System.Runtime.InteropServices.DllImport("dwmapi.dll")]
public extern static int DwmIsCompositionEnabled(ref int en);
}
}
That's because you have
this.BackColor = Color.Magenta;
this.TransparencyKey = Color.Magenta;
Remove line with transparency. This also happens in WindowsForms. I think it's by design, not bug.