CS0122 happening where it shouldn't.  
Author Message
p0lar_bear





PostPosted: Visual C# General, CS0122 happening where it shouldn't. Top

I'm writing some code to read a process's memory. I've used it as a code bit and it works; however, I can't seem to get it working in a DLL file. If I reference the DLL and compile, the compiler throws a CS0122; saying that the class I'm attempting to use, ReadWrite, is not public: "'ProcessMemory.ReadWrite' is inaccessible due to its protection level"

However, my code for the DLL says quite otherwise: http://www.hide-link.com/

As you can see, my class, and its functions, are quite public. What is the deal here

EDIT: Removed the edited code snipped I posted, and replaced it with a link to the source. 



Visual C#9  
 
 
TaylorMichaelL





PostPosted: Visual C# General, CS0122 happening where it shouldn't. Top

You should not declare public methods that you are implementing as extern. Extern is used to identify methods that are declared in an assembly but are implemented elsewhere. For example this modifier is applied to P/Invoke methods and to methods implemented in the CLR native code. I'm somewhat impressed that it even compiled to begin with.

If you are trying to map back to the unmanaged code then you should create a class that exposes public, managed methods and types for use in .NET code and then use private methods to actually call the private extern methods through P/Invoke. Ideally you can move the private logic into a separate internal or private/nested class if your public class has additional work to do.

public class ReadWrite : IDisposable
{
public ReadWrite ( int process Id )
{
...
}

public ReadWrite ( Process process )
{
...
}

public void ReadProcessMemory ( IntPtr baseAddress, byte[] buffer, ref long bytesRead )
{
...
}

public void WriteProcessMemory ( IntPtr baseAddress, byte[] buffer, ref long bytesWritten )
{
...
}

//Or move to a nested class or internal class
[DllImport("kernel32.dll")]
private static extern IntPtr OpenProcess ( uint dwDesiredRights, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandles, uint dwProcessId );

private ProcessHandle m_Handle; //ProcessHandle is a class you create derived from SafeHandle
}

The syntax is not quite right but you get the jist. Hide the unmanaged code from .NET code. Use managed types and custom types to expose the functionality. Use SafeHandle when working with unmanaged handles.

Since opening, manipulating and closing a process requires a handle it should be implemented as a class instance rather than as static methods. By implementing IDisposable users know to dispose of the object when done. Use .NET types whenever possible to keep things clean.

Michael Taylor - 12/12/06


 
 
p0lar_bear





PostPosted: Visual C# General, CS0122 happening where it shouldn't. Top

Your syntax is entirely different than what has worked, perhaps i didn't post enough code. I found the class on a tutorial site that showed you how to make a trainer for Minesweeper, and I also added some constants based on the same concept, but the vode was VB .NET.

This is the code I have; I'm still learning memory operations, but I know the basics. ;)
http://rafb.net/paste/results/H8nxZU57.html

EDIT: I noticed that I have an extern propetry applied to one of the constants; ignore that.


 
 
p0lar_bear





PostPosted: Visual C# General, CS0122 happening where it shouldn't. Top

Well, I had to read, re-read, and re-re-read that until I finally got it. I'm a little slow at this, still learning.

Thanks for the lesson and the code cleanup. :)