Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi, I have developed a c# dll that makes http reqest using System.Net namespace. If I call the dll methods from an c# programm everything workes fine but i need to use this dll from native c++ code. For this i used COM interop service. All goes fine until is called GetResponseAsync() function. At this poin the program crashes and return Access violation reading location 0x00000000.
C#
  public async Task PostRequest(string URL, string jsonRequestStr, string accessToken)
  {
          HttpWebRequest httpPostRequest = (HttpWebRequest)WebRequest.Create(URL);
          httpPostRequest.Method = "POST";
          if (accessToken != "")
          {
              httpPostRequest.Headers.Add(string.Format("Authorization: Bearer {0}", accessToken));
          }

          byte[] requestBuffer = Encoding.UTF8.GetBytes(jsonRequestStr);
          httpPostRequest.ContentLength = requestBuffer.Length;
          httpPostRequest.ContentType = "application/json";

          using (var requestStream = new StreamWriter(httpPostRequest.GetRequestStream()))
          {
              await requestStream.WriteAsync(jsonRequestStr);
          }

          WebResponse tokenResponse = httpPostRequest.GetResponse();
          using (StreamReader reader = new StreamReader(tokenResponse.GetResponseStream()))
          {
              string responseText = await reader.ReadToEndAsync();

              if (responseText != "")
              {
                  this.jResponseDecoded = JObject.Parse(responseText);
              }

          }
}


What I have tried:

I have tried to make Http request with HttpClient but also crashed.
Posted
Updated 3-Mar-21 22:37pm

1 solution

Firstly,you aren't calling GetResponseAsync in that code.
Secondly, half the other stuff we have no idea what exactly they are: JObject, what is returned by your GetResponse call.
Thirdly, why are you preparing a byte array version of your string, and then not using it? You send the Unicode String instead, which may be part of your problem - it's not necessarily the same size as the buffer you told it you were using.

And we can't find out any of that stuff!
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
Member 14649480 4-Mar-21 5:17am    
Hi, I appreciate that you answered to me but isn't about debugging and also progam crashes if i use GetResponseAsync. I repet that the dll workes if i call if from a c# progam. It crashes only when i use it from c++. It's not about how i use it from c++ cuz other functions from dll workes fine in this context.
Shao Voon Wong 4-Mar-21 19:58pm    
Shouldn't you be showing the non-working C++ code instead? Without reading the C++ code, no one can be sure of what went wrong.
Member 15211483 22-May-21 14:05pm    
i am also facing same issue in my current project
Is this resolved?
Please let me know
OriginalGriff 22-May-21 14:07pm    
Did you read any of what I said two months ago?
nearly all of it applies to your app as well ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900