Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a small application in C# .NET 2008, in which i am trying to use VC++ .NET 2008 Dll.

In Dll, one class is created, in that one function and one variable are added.
In C#, Window form application, i have added VC Dll reference.

Now i can create object of Dll, but i am unable to access it function or variable.
It give error message as -

"____ does not contain a definition for ____ and no extension method ____ accepting a first argument of type _____ could be found (are you missing a using directive or an assembly reference)"


Posting here my code snippet
---------------------------------------------------------------
VC 2008 code -

C++
#include "stdafx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

namespace DllNameSp
{       
    public class Test
    {
    public:         
        int abc;
        int iTest()
        {
            int z=0,x=0,y=0;
            return z=x+y;
        }
    };
}

-------------------------------------------------------
C# code

C#
        private void Form1_Load(object sender, EventArgs e)
{
    DllNameSp.Test obj1 = new DllNameSp.Test();
    obj1.abc = 90;
    int k = obj1.iTest();   
}
Posted
Updated 18-Nov-11 23:28pm
v3
Comments
Richard MacCutchan 19-Nov-11 6:33am    
Can you post the exact error message you receive, rather than the redacted one above?

You should create managed object in your mixed-mode C++ assembly.

That is, public ref class Test.

C# will not be able to call unmanaged code directly. The recommanded way is that any public class are managed one.

There is a lot more to learn if you want to mix managed and unmanaged code... Particulary, you will have to learn how constructors, destructors and finalizers works and also gcroot helper class could be usefull if you need to keep a managed handle inside an unmanaged class.

You might also want to known that there are 3 clr compilation mode available in C++. The default one is what you will generally used for mixed code. The is also a pure and a safe mode that are more restrictive.
 
Share this answer
 
Comments
nhasmita 20-Nov-11 23:42pm    
Thank you very much.
I have added 'ref' and my application was free from errors. It was working :-)

And Thank you for your guideline for mix managed and unmanaged code.
You cannot call C++ functions without defining them via the DllImport attribute as described in this P/Invoke tutorial[^].
My mistake, I misread the original question.
 
Share this answer
 
v2
Comments
nhasmita 19-Nov-11 7:10am    
Did you got where i am going wrong ?
How to resolve these error?
Richard MacCutchan 19-Nov-11 7:27am    
Have you added the C++ DLL to your project references, and included the correct using statement? As I said above, please show us the exact error message you receive from the compiler.
nhasmita 20-Nov-11 23:30pm    
'DllNameSp.Test' does not contain a definition for 'abc' and no extension method 'abc' accepting a first argument of type 'DllNameSp.Test' could be found (are you missing a using directive or an assembly reference?)

This error for assigning value to variable 'abc'.
-----------------------------------------------------------------------

'DllNameSp.Test' does not contain a definition for 'iTest' and no extension method 'iTest' accepting a first argument of type 'DllNameSp.Test' could be found (are you missing a using directive or an assembly reference?)



This error for calling function iTest().
-----------------------------------------------------------------------

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