Crashing when accessing "g_pVGuiSurface" in GMod .dll module

Hi, I am trying to port my Lua code to C++ to improve perfomance.

In my visual studio project I have included GMod Lua headers ( and Source SDK 2007 headers and libraries. Everything compiles fine, but when this line executes the game crashes.

g_pVGuiSurface->DrawTexturedRect(x, y, x + w, y + h);

As I found out every call to “g_pVGuiSurface” (or “vgui::surface()”) makes game to crash.
Other functions like “Msg()” work fine, so I just don’t know what goes wrong.

The crash memory dump file information:

Process Name:			hl2.exe : D:\Program Files (x86)\Steam\steamapps\common\GarrysMod\hl2.exe
Process Architecture:		x86
Exception Code:			0xC0000005
Exception Information:		The thread tried to read from or write to a virtual address for which it does not have the appropriate access.
Heap Information:		Not Present

I guess that the headers from Source SDK 2007 may not be suitable for Garrys Mod, but these are the only headers that I’ve got.

For starters, you can get a newer SDK here: GitHub - ValveSoftware/source-sdk-2013: The 2013 edition of the Source SDK

Make sure you are actually creating the vgui interface. It’s not just gonna appear in that variable by magic.

Thanks for the link.
I’m trying to create a function that draws 2d objects on screen (similar to lua analogue “surface.DrawTexturedRect()”). What am I missing?

Here’s a mockup of what you want.

CDllDemandLoader vguisurface_module("vguimatsurface");

	CreateInterfaceFn func = vguisurface_module.GetFactory();
	g_pVGuiSurface = (vgui::ISurface*)func(VGUI_SURFACE_INTERFACE_VERSION, NULL);
	// Now you should be able to do stuff with g_pVGuiSurface

I haven’t tested this. You may need to try other dll names.

It worked! Thank you!

Btw, for anyone minding this question in future, if you are compiling using Visual Studio 2015 and getting error “LNK2001 unresolved external symbol __vsnwprintf” you should add “legacy_stdio_definitions.lib” in Configuration Properties -> Linker -> Input -> Additional Dependencies.