Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to static object yields Invalid #1252

Open
leotohill opened this issue Apr 18, 2024 · 2 comments
Open

Access to static object yields Invalid #1252

leotohill opened this issue Apr 18, 2024 · 2 comments

Comments

@leotohill
Copy link

Why does this fail at the indicated line?

var targetType = module.GetTypeByName("x.y.classname"); 
if (targetType == null)
{
	continue;
}
var staticField = targetType.GetStaticFieldByName("FieldName"); 

if (!staticField.IsInitialized(runtime.AppDomains[0]))
{
	Console.WriteLine("Static field is not initialized.");
	return;
}
var objAddress = staticField.GetAddress(runtime.AppDomains[0]);
if (objAddress == 0)
{
	Console.WriteLine("Obj address  is 0");
	return;
}
var targetObj = runtime.Heap.GetObject(objAddress);
if (targetObj.IsNull)
{
	Console.WriteLine("target object is null.");
	return;
}
if (!targetObj.IsValid)
{
	Console.WriteLine("target object is not valid.");  // FAILS HERE  <<<<<<<<<<<
	return;
}
if (targetObj.Type == null)
{
	Console.WriteLine("target object.Type is null."); 
	return;
}
Console.WriteLine("success so far.");
@richardfencel
Copy link

richardfencel commented Jun 2, 2024

Try:

targetObject = targetType.Heap.GetObject(objAddress)

Oops! I think this may be wrong. See the one below instead.

@richardfencel
Copy link

richardfencel commented Jun 2, 2024

Here is another option:

var targetObject = staticfield.ReadObject(_runtime.AppDomains[0]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants