The return value tells you that the buffer you provided to the function is not large enough to contain the data that it wants to return. You need to call InternetGetCookie
twice: drive once at a rate of 0 to find out how big the buffer is; and the second time with a buffer of the right size.
Also, the P / Invoke signature is incorrect; StringBuilder
must not be a ref
parameter (and the EntryPoint
parameter is EntryPoint
because it does not specify the correct entry point name).
Declare the function as follows:
[DllImport("wininet.dll", SetLastError = true)] public static extern bool InternetGetCookie(string lpszUrl, string lpszCookieName, StringBuilder lpszCookieData, ref int lpdwSize);
Then name it like this:
// find out how big a buffer is needed int size = 0; InternetGetCookie("http://example.com", null, null, ref size); // create buffer of correct size StringBuilder lpszCookieData = new StringBuilder(size); InternetGetCookie("http://example.com", null, lpszCookieData, ref size); // get cookie string cookie = lpszCookieData.ToString();
source share