VMware {code} Community
MarkWTaylor
Contributor
Contributor

How do I call the vmGuestLib.dll functions ?

I want to write some guest-level code and I have a question ....

How do I call the vmGuestLib.dll functions from .NET - preferably C# as I'm not a VB coder. The DLL is obviously a C module but I don't have any header files or mapping for the exported functions to go on. The docs just name the functions - they don't give any templates for calling the actual code.

Reply
0 Kudos
5 Replies
_t
Contributor
Contributor

first of all you need to download the guest sdk from:

http://www.vmware.com/support/developer/

the download is a zip-archive that contains documentation and the header file for the vmguestlib.dll named "vmGuestLib.h" you might looking for.

you're correct that the DLL is obviously a C module.

To call it from C# you have to translate the header file to C# \[DllImports...] using .NET P/Invoke to call the "unmanaged" functions.

If your not familiar with P/Invoke take a look at MSDN or this article:

http://msdn.microsoft.com/msdnmag/issues/03/07/NET/

Reply
0 Kudos
MarkWTaylor
Contributor
Contributor

Thanks for that - I obviously got caught out by the fact that the documentation just mentions that the vmguestlib.dll is installed with vmware tools - it doesn't mention that there's a separate SDK package.

Reply
0 Kudos
MarkWTaylor
Contributor
Contributor

OK - I'm stuck - does anybody have a simple C# example of how to call the guest SDK ?

The problem I have is that the OpenHandle()/CloseHandle routines appear to work fine (as does GetErrorText) but when I call UpdateInfo() it bombs with " Object reference not set to an instance of an object." - as the only argument passed is the Handle then I assume that the handle is invalid. If I call any of the other Getxxxxx() routines I get meaningless values - presumably because the various metrics haven't been initialised.

Reply
0 Kudos
MarkWTaylor
Contributor
Contributor

more info required

Reply
0 Kudos
_t
Contributor
Contributor

i haven't esx server installed so i could not test the code,

but this is how i would have done it.

create a console application project an paste this code:

using System;

using System.Text;

using System.Runtime.InteropServices;

namespace ConsoleApplication2

{

class GuestTest

{

static void

Main(string[] args)

{

try

{

using(VMGuestLib lib = new VMGuestLib())

{

Console.WriteLine(string.Format("mem usage: \{0} of \{1} MB", lib.getMemActiveMB(), lib.getMemLimitMB()));

}

}

catch(Exception e)

{

Console.WriteLine(string.Format("guest test failed: \{0}\n\{1}", e.Message, e.StackTrace));

}

}

}

class VMGuestLib

: IDisposable

{

private readonly IntPtr m_Handle;

public VMGuestLib()

{

EnsureSuccess(VMGuestLib_OpenHandle(out this.m_Handle));

this.updateInfo();

}

~VMGuestLib()

{

this.Dispose(false);

}

public void

updateInfo()

{

EnsureSuccess(VMGuestLib_UpdateInfo(this.m_Handle));

}

public UInt32

getMemLimitMB()

{

UInt32 memLimitMB;

EnsureSuccess(VMGuestLib_GetMemLimitMB(this.m_Handle, out memLimitMB));

return memLimitMB;

}

public UInt32

getMemActiveMB()

{

UInt32 memActiveMB;

EnsureSuccess(VMGuestLib_GetMemActiveMB(this.m_Handle, out memActiveMB));

return memActiveMB;

}

#region IDisposable Member

public void

Dispose()

{

this.Dispose(true);

GC.SuppressFinalize(this);

}

private void

Dispose(bool disposing)

{

VMGuestLib_CloseHandle(this.m_Handle);

}

#endregion

#region Error Checking Helper

public static void

EnsureSuccess(VMGuestLibError error)

{

if (error != VMGuestLibError.VMGUESTLIB_ERROR_SUCCESS)

throw new VMGuestLibException(error);

}

#endregion

#region Guest API Imports

\[DllImport("vmGuestLib.dll")]

private static extern VMGuestLibError

VMGuestLib_OpenHandle(out IntPtr handle);

\[DllImport("vmGuestLib.dll")]

private static extern VMGuestLibError

VMGuestLib_CloseHandle(IntPtr handle);

\[DllImport("vmGuestLib.dll")]

private static extern VMGuestLibError

VMGuestLib_UpdateInfo(IntPtr handle);

\[DllImport("vmGuestLib.dll")]

private static extern VMGuestLibError

VMGuestLib_GetMemActiveMB(

IntPtr handle,

out UInt32 memActiveMB);

\[DllImport("vmGuestLib.dll")]

private static extern VMGuestLibError

VMGuestLib_GetMemLimitMB(

IntPtr handle,

out UInt32 memLimitMB);

#endregion

}

public enum VMGuestLibError

{

VMGUESTLIB_ERROR_SUCCESS = 0, // No error

VMGUESTLIB_ERROR_OTHER, // Other error

VMGUESTLIB_ERROR_NOT_RUNNING_IN_VM, // Not running in a VM

VMGUESTLIB_ERROR_NOT_ENABLED, // GuestLib not enabled on the host.

VMGUESTLIB_ERROR_NOT_AVAILABLE, // This stat not available on this host.

VMGUESTLIB_ERROR_NO_INFO, // UpdateInfo() has never been called.

VMGUESTLIB_ERROR_MEMORY, // Not enough memory

VMGUESTLIB_ERROR_BUFFER_TOO_SMALL, // Buffer too small

VMGUESTLIB_ERROR_INVALID_HANDLE, // Handle is invalid

VMGUESTLIB_ERROR_INVALID_ARG, // One or more arguments were invalid

}

public class VMGuestLibException

: Exception

{

public VMGuestLibException(VMGuestLibError error)

: base(VMGuestLib_GetErrorText(error).ToString())

\{}

#region Guest API Imports

\[DllImport("vmGuestLib.dll")]

\[return:MarshalAs(UnmanagedType.LPStr)]

private static extern string

VMGuestLib_GetErrorText(VMGuestLibError error);

#endregion

}

}

Reply
0 Kudos