TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Arijeet Ghosh
NA
210
36.7k
Made a function in C but how to...?
Jul 3 2020 10:49 PM
he examples given by Sir....if I change a little bit then the program runs OK.
But I don't know how to store the result in C#. Please assist.......
So I mention the code here:-
VC++ Code:-
In Ctest1.h
#include<stdio.h>
#include "pch.h"
#pragma once
class __declspec(dllexport) Ctest1
{
public:
Ctest1();
~Ctest1();
char* func();
};
In Ctest1.cpp
#include "pch.h"
#include "Ctest1.h"
char* Ctest1::func()
{
char sname[] = { "kkk" };
return sname;
}
Ctest1::Ctest1()
{}
Ctest1::~Ctest1()
{}
In Caller.h
#include "Ctest1.h"
#include "pch.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern __declspec(dllexport) Ctest1* _Create();
extern __declspec(dllexport)void dispose(Ctest1* _pObject);
extern __declspec(dllexport)char* func(Ctest1* _pObject);
#ifdef __cplusplus
}
#endif
In Caller.cpp:-
#include "pch.h"
#include "Caller.h"
Ctest1* _Create()
{
//Ctest1 c = new Ctest1();
return new Ctest1();
}
void dispose(Ctest1* _pObject)
{
if (_pObject != NULL)
{
delete _pObject;
_pObject = NULL;
}
}
char* func(Ctest1* _pObject)
{
if (_pObject != NULL)
{
char* str = _pObject->func();
return str;
}
}
And in C# Code:-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
[DllImport("First.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr _Create();
[DllImport("First.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void dispose(IntPtr pCtest1);
[DllImport("First.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr func(IntPtr pTest);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IntPtr ptestClass = _Create();
IntPtr p= func(ptestClass);
/// Now how to get the string in C-funcion
dispose(ptestClass);
}
}
}
So sir how to get the string returned from C++ and store it in C#
Reply
Answers (
2
)
Reset Link on mail .net core.
how to handle versioning in .NET Core