By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

Problems using gcroot -> AccessViolation when calling managed functions in native C++

P: 3
Getting this error upon running my .exe ... Compilation goes FINE :/

Here's my CLR code:

header:

Expand|Select|Wrap|Line Numbers
  1. // AddingWrapper.h
  2.  
  3. #pragma once
  4. #include "stdafx.h"
  5.  
  6. class AddingWrapperPrivate;
  7.  
  8.  
  9. class __declspec(dllexport) AddingWrapper {
  10.  
  11. private: AddingWrapperPrivate* _private;
  12. public: AddingWrapper();
  13.         int Adding(int* x, int* y);
  14.         ~AddingWrapper();
  15.  
  16. };
source code:
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2.  
  3. #include "AddingWrapper.h"
  4.  
  5. #using "Class1.dll"
  6. #include <msclr\auto_gcroot.h>
  7. using namespace System::Runtime::InteropServices;
  8.  
  9. class AddingWrapperPrivate {
  10. public: msclr::auto_gcroot<Add^> add;
  11. };
  12.  
  13.     AddingWrapper::AddingWrapper()
  14.     {
  15.         _private = new AddingWrapperPrivate();
  16.         _private->add = gcnew Add();
  17.     };
  18.    int AddingWrapper::  Adding(int* x, int* y) {
  19.         return _private->add->Adding(*x, *y);
  20.     };
  21.     AddingWrapper::~AddingWrapper()
  22.     {
  23.         delete _private;
  24.     };
  25.  
  26.  

Test application :

// Test.cpp : Defines the entry point for the console application.
//
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include "AddingWrapper.h"
  3. #include <iostream>
  4. int main()
  5. {
  6.     int *a = 0;
  7.     int *b = 0;
  8.     AddingWrapper *add;
  9.     int results =  add->Adding(a,b);
  10.     std::cout << "here is the result";
  11.     std::cout << results;
  12.     return 0;
  13. }
  14.  
It throws the error at add-> Adding(a,b). Some help would be greatly appreciated
Jun 8 '16 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
At add-> Adding(a,b) the variables a and b are 0.
Jun 9 '16 #2

P: 3
Shouldn't matter no? Adding(0,0) should pass it to the vb code that only adds 0+0, no? Which is by no mean an illegal operation. Plus, if that was the issue, shouldn't it then throw me another exception and not accessViolation?
Jun 9 '16 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
a and b are pointers. They are to contain the address of integers. A 0 address is invalid, hence the access violation.

There needs to be two additional variables which are integers. Then you put the addresses of those integers into these pointers. Then you put values in the integers. And finally, you can call the adding function.
Jun 9 '16 #4

Post your reply

Sign in to post your reply or Sign up for a free account.