2

I have the following header and CPP:

"Utils.h"

__declspec(dllexport) static char* GetRamMegabytes(char* &FreeMemory);

"Utils.cpp"

char* Utils::HardWare::GetRamMegabytes(char*  &FreeMemory)
 {
    char*  TotalMb = "";
    MEMORYSTATUSEX statex;
    statex.dwLength = sizeof(statex);
    GlobalMemoryStatusEx(&statex);
    float freeMemFloat = ((float)statex.ullAvailPhys/1024/1024);
    float value =((float)statex.ullTotalPhys/1024/1024);
    sprintf(FreeMemory,"%f",value);
    sprintf(TotalMb,"%f",freeMemFloat);
    return TotalMb;
 }

I have my DLL compiled and I'm trying to make a PInvoke from C# with the following code:

[DllImport("LndNativeAssembly.dll", EntryPoint = "?GetRamMegabytes@HardWare@Utils@@SAPADAAPAD@Z", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    public static extern IntPtr GetRamMegaBytes(IntPtr freemem);

I'm trying to call the native function using:

IntPtr free = IntPtr.Zero;
IntPtr res = GetRamMegaBytes(free);

And I get this error:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

5
  • 1
    FreeMemory needs to be passed in with space already allocated and you need to allocate TotalMb, assigning it to "" is useless. Also instead of returning a value through a paramater and a return statement have you considered returning a custom struct and adding an accompanying free method. Commented Nov 12, 2012 at 14:47
  • There are 3 serious bugs in this code. Do favor the .NET class that gives you this exact same info: msdn.microsoft.com/en-us/library/… Commented Nov 12, 2012 at 14:51
  • Again I repeat. This is about HOW invoking a native DLL. Not about how to do it with .NET. I KNOW .Net give you that information. Commented Nov 12, 2012 at 14:52
  • This doesn't have anything to do with pinvoke. You'll need to learn C programming. Commented Nov 12, 2012 at 14:56
  • @CarlosLande I believe what they are saying is you need lean more about C and C++ before trying to complicate things with P/Invoke. This would of been easier to resolve if you were doing straight C or C++. Commented Nov 12, 2012 at 15:47

2 Answers 2

2

This is writing to unallocated memory.

sprintf(TotalMb,"%f",freeMemFloat);

You can either allocate memory using new in the routine and free it in the caller, or you can have a fixed buffer in the routine (not thread-safe).

Sign up to request clarification or add additional context in comments.

5 Comments

Why are you returning two similar values in two different ways? Why not pass in two buffers?
Because I wanted to know how to pInvoke it this way :)
Im learning c++, only been here a week . Could you give me an example of how should it be done?. Best way possible
Better is to use strings. Does it have to be char*?
No stark. I really prefer to use std::string but I got errors in the pInvoke
2

This is because Free is the target in which the function is trying to write, and you are passing NULL, that results in a GPF. The problem is how you wrote the PInvole signature. Try with:

public static extern IntPtr GetRamMegaBytes(StringBuilder freemem);

and pass a created string builder to the function.

1 Comment

I got the same Error--> Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.