I'm having trouble calling unmanaged dll function from the windows win32 apis.
I've tried many ways, but the following was my first and what I think should work.
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Win32DllImports
{
class Program
{
[DllImport("Urlmon",
EntryPoint = "ObtainUserAgentString",
CallingConvention = CallingConvention.StdCall,
CharSet = CharSet.Ansi)]
private static extern uint ObtainUserAgentString(uint dwOption,
char[] userAgent, ref
uint length);
static void Main(string[] args)
{
char[] buffer = new char[512];
uint size = new uint();
uint rtn = ObtainUserAgentString(0, buffer, ref size);
string frombuffer = new string(buffer);
Debug.WriteLine(rtn); // 2147942414 (E_OUTOFMEMORY)
Debug.WriteLine(frombuffer); // prints nothing, not even new line char
Debug.WriteLine(size); // 156
}
}
}
Of course it does not work, the comments show what the results are, an out of memory error, buffer not filled with what should be a 70 char user agent string.
Can anyone see what I'm doing wrong?