I am using Gma.System.MouseKeyHook and getting the following exception:
Managed Debugging Assistant 'CallbackOnCollectedDelegate'
Message=Managed Debugging Assistant 'CallbackOnCollectedDelegate' : 'A callback was made on a garbage collected delegate of type 'Gma.System.MouseKeyHook!Gma.System.MouseKeyHook.WinApi.HookProcedure::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.'
I've tried to handle the function calls and subscription. However, the issue still persists. Also I try to run it many times, occasionally it gives a 'NullReferenceException' as well. It also confused me a lot, maybe those issues are correlated.
using Gma.System.MouseKeyHook;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
// Uses a MouseKeyHook Library license at
https://github.com/gmamaladze/globalmousekeyhook/blob/master/LICENSE.txt
namespace TransparentClickTest {
public partial class Form1 : Form {
public Form1() {
//GC.TryStartNoGCRegion(100);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
BackColor = Color.Red;
TransparencyKey = Color.Red;
InitializeComponent();
OnDown();
}
protected virtual void OnUp() {
Hook.GlobalEvents().MouseUp += (sender, e) => {
try {
label1.Text = "Mouse Up!!!";
Hook.GlobalEvents().Dispose();
OnDown();
}
catch(Exception e2) {
Hook.GlobalEvents().Dispose();
OnDown();
}
};
}
protected virtual void OnDown() {
Hook.GlobalEvents().MouseDown += (sender, e) => {
try {
label1.Text = $"Mouse {e.Button} Down at {e.X}, {e.Y}";
Opacity = 1;
Hook.GlobalEvents().Dispose();
OnUp();
}
catch(Exception e1) {
Hook.GlobalEvents().Dispose();
OnUp();
}
};
}
private void PictureBox1_Click(object sender, EventArgs e) {
}
private void Label1_Click(object sender, EventArgs e) {
}
}
}
add this: private static IKeyboardMouseEvents HookEvents = null;
use HookEvents.MouseDown replace Hook.GlobalEvents().MouseDown
Related
So today I tried dll injection to game (assult cube) to test my experience with my job.
But I got problems with Win32 Exception: 0xc0000005.
My code looks like:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Memory;
namespace AssultCube_Cheat
{
public partial class Form1 : Form
{
Mem assultcube = new Mem();
public static string RifleAmmo = "ac_client.exe+0x0017E0A8";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int PID = assultcube.GetProcIdFromName("ac_client");
if (PID > 0)
{
assultcube.OpenProcess(PID);
Thread WA = new Thread(writeAmmo) { IsBackground = true };
WA.Start();
}
}
private void writeAmmo()
{
while (true)
{
if (checkBox1.Checked)
{
assultcube.WriteMemory(RifleAmmo, "int", "80");
Thread.Sleep(2);
}
Thread.Sleep(2);
}
}
}
}
I tried updating my computer drivers, upgrading my system to Windows 11 but problem didnt fix.
Hi I have 2 questions.
My timer stops immediately and if it doesn't it does not pick up the given interval.
CODE:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ClashRoyaleHack
{
public partial class GoldCMD : Form
{
public GoldCMD()
{
InitializeComponent();
timer2.Interval = 10000;
timer2.Tick += new EventHandler(timer2_Tick);
timer2.Start();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void timer2_Tick(object sender, EventArgs e)
{
this.Close();
}
}
}
On creating the code I notices a slight difference. I am using almost identical forms with the same content but different names and different timer names etc. But one does work with the timer and same code (using ofcourse different names for the timer and form etc) but the other doesn't...
Really weird. Hopefully you guys can help me out. The code above does not have the private void GoldCMD_Load(object sender, EventArgs e) But just the normal private void GoldCMD(object sender, EventArgs e) maybe that has something to do with it.
Code of the one that does work:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ClashRoyaleHack
{
public partial class GemsCMD : Form
{
public GemsCMD()
{
InitializeComponent();
}
private void GemsCMD_Load(object sender, EventArgs e)
{
timer1.Interval = 7500;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.Close();
}
}
}
The Solution:
It is conceivable that the timer may "tick" once upon starting and considering your code closes it immediately after a tick, it is likely the source of your issue. Otherwise if that is not the reasoning, then you are still killing it at its first tick regardless of time.
I am trying to chase a memory leak in a multiform C#.Net application, on Windows 7, VS 2008.
I found this SO post, which indicates that the second form's finalizer should be automatically called
this.Dispose() doesn't release memory used by Form after closing it.
However, it isn't working for me.
Each time a pop up the second form (which contains a large string, and a PictureBox), the amount of memory the app uses increases, even when I force a GC. Interestingly, the String's finalizer IS being called.
I've add a log to both form's Dispose methods; Form2's dispose is being called.
The code is
Form1 (main form)
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 MemoryLeakTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
~Form1()
{
System.Diagnostics.Debugger.Log(0, "", "Form1.Destructor has been called.\n");
}
private void GC_Click(object sender, EventArgs e)
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
}
private void OpenForm_Click(object sender, EventArgs e)
{
Form secondForm = null;
if (secondForm == null)
{
secondForm = new Form2();
secondForm.Show();
}
secondForm = null;
}
private void UpdateMemory_Click(object sender, EventArgs e)
{
long memory = System.GC.GetTotalMemory(false);
this.MemoryUsage.Text = "Memory usage: " +
String.Format("{0:n}", memory) +
" bytes";
}
private void AllocateString_Click(object sender, EventArgs e)
{
// StringWrapper wrapper = new StringWrapper();
}
}
public class StringWrapper
{
String str = new String('*', 1024 * 1024);
~StringWrapper()
{
System.Diagnostics.Debugger.Log(0, "", "StringWrapper finalizer has been called.\n");
}
}
}
and Form2 is
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 MemoryLeakTest
{
public partial class Form2 : Form
{
StringWrapper wrapper = new StringWrapper();
public Form2()
{
InitializeComponent();
this.pictureBox1.Image = Image.FromFile("C:/Windows/Web/Wallpaper/Windows/img0.jpg");
}
~Form2()
{
System.Diagnostics.Debugger.Log(0, "", "Form2.Finalizer has been called.\n");
}
}
}
I haven't included the designer generated code, but can add it if need be
I am creating a application form to view/change a tag from a software called InTouch.
I added the dll as a reference and I would like to use the Read(string tagName) fct in the IOM.InTouchDataAccess. VS does not see the fct Read when I write InTouchWrapper TagType = new read(). It only sees InTouchWrapper as I wrote in the code which gives me the error IOM.InTouchDataAccess.InTouchWrapper' does not contain a constructor that takes 0 arguments
I don't understand why is this happening. I am running the InTouch software while coding, maybe there is an access conflict with the software.
MyCode
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 IOM.InTouchDataAccess;
namespace TagBrowser
{
public partial class TagBrowser : Form
{
public TagBrowser()
{
InitializeComponent();
}
private void TagBrowser_Load(object sender, EventArgs e)
{
}
private void TagBox_TextChanged(object sender, EventArgs e)
{
}
private void TypeBox_SelectedIndexChanged(object sender, EventArgs e)
{
InTouchWrapper TagType = new InTouchWrapper();
}
The dll
using System;
using System.Collections.Generic;
using System.Text;
using NDde.Client;
namespace IOM.InTouchDataAccess
{
public class InTouchDdeWrapper : IDisposable
{
private int DDE_TIMEOUT = 60000;
private DdeClient _ddeClient;
public InTouchDdeWrapper()
{
_ddeClient = new DdeClient("View", "Tagname");
}
~InTouchDdeWrapper()
{
Dispose();
}
public void Initialize()
{
_ddeClient.Connect();
}
public string Read(string tagName)
{
return _ddeClient.Request(tagName, DDE_TIMEOUT).Replace("\0", "");
}
I'm putting this here in case somebody else would get the same problem:
Are you sure it's the correct dll you referenced? Try to open the
exact referenced dll in a decompiler (JustDecompile free,
Reflector or dotPeek free) and see if it's the code you
expect.
I know this is a pretty common error, usually having to do with the onStart method of a windows service, but I can't figure out why this one isn't working.
Heres a stack trace of the error via windows event viewer:
at System.Diagnostics.EventLog.FindSourceRegistration(System.String, System.String, Boolean, Boolean)
at System.Diagnostics.EventLog.SourceExists(System.String, System.String, Boolean)
at System.Diagnostics.EventLog.SourceExists(System.String)
at ArchivalPurgeService.ArchivalPurge..ctor()
at ArchivalPurgeService.Program.Main()
and here is my program:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
namespace ArchivalPurgeService
{
public partial class ArchivalPurge : ServiceBase
{
public ArchivalPurge()
{
try
{
InitializeComponent();
}
catch (Exception ex)
{
}
}
protected override void OnStart(string[] args)
{
try
{
timer2 = new Timer();
timer2.Enabled = true;
timer2.Interval = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["RuntimeFrequency"]);
}
catch (Exception ex)
{
}
}
private Queue<Job> QueryDBForJobs()
{
int i = 0;
return new Queue<Job>();
}
protected override void OnStop()
{
}
private void timer2_Elapsed(object sender, ElapsedEventArgs e)
{
QueryDBForJobs();
}
}
}
And I tried to run it even with everything commented out, and I'm getting the same issue. Could this just be a problem with the installation, maybe? I'm using an installer which I created almost exactly based on the MSDN how-to for creating windows services. I'm obviously also building/uninstalling/reinstalling after every code change.
Apparently the issue was with the install, not the code. When I install via installutil everything works fine.