Create dll File in c++ and c# to Use in an Application Project on Visual Studio 2017 52

Опубликовано: 22 Апрель 2026
на канале: Arif Mahmood
5,969
26

Shows how to create a #dll file based project in c plus plus and c sharp and test/use with an application. How to #convert a simple project to a project with a dll implementation in visual studio 2017. Shows how to create a .dll file and use it to #run a application.


c++ code:

// use_dll.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


//int main()
//{
// return 0;
//}

#include <iostream> //for cout
#include <fstream> // for ofstream/ifstream
#include <string> //for <<and getline
#include<list> //for lists
#include"Header.h"


int main(int argc, char *argv[])
{
std::string in_file;
in_file = "test.txt";

std::string out_file;
out_file = "test.txt";

std::list<std::string> elements;
elements.clear();

header::pullout_contents(in_file, elements);

header::show_contents(std::cout, elements);

header::pushin_contents(out_file + ".out", elements);

std::cin.get();
return 0;
}


//Source.cpp is another source file
//Source1.cpp is another source file
#include <iostream> //for cout
#include <fstream> // for ofstream/ifstream
#include <string> //for << and getline
#include<list> //for lists
#include"Header.h"

void header::pullout_contents(std::string in_file, std::list<std::string> &elements)
{
std::ifstream input_file(in_file.c_str());

elements.clear();
std::string line;
for (int line_no = 1; std::getline(input_file, line); ++line_no)
{
elements.push_back(line);
}
input_file.close();
}

void header::show_contents(std::ostream &out, const std::list<std::string> &elements)
{
std::list<std::string>::const_iterator iter = elements.begin();
for (; iter != elements.end(); ++iter)
{
out << (*iter) << std::endl;
}
}

void header::pushin_contents(std::string out_file, const std::list<std::string> &elements)
{
std::ofstream output_file(out_file.c_str());
show_contents(output_file, elements);// using polymorphism
output_file.close();
}



//Header.h is a header file
#pragma once
#ifndef GUARD_TOKENS_H
//#define GUARD_TOKENS_H
#define GUARD_TOKENS_H _declspec(dllexport)
#else
#define GUARD_TOKENS_H _declspec(dllimport)
#endif
class header
{
public:
//static void pullout_contents(std::string in_file, std::list<std::string> &elements);
//static void show_contents(std::ostream &out, const std::list<std::string> &elements);
//static void pushin_contents(std::string out_file, const std::list<std::string> &elements);
static GUARD_TOKENS_H void pullout_contents(std::string in_file, std::list<std::string> &elements);
static GUARD_TOKENS_H void show_contents(std::ostream &out, const std::list<std::string> &elements);
static GUARD_TOKENS_H void pushin_contents(std::string out_file, const std::list<std::string> &elements);
};




c# code:


//Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;//to use StreamReader
using CreateDll;
namespace UseDll
{
class Program
{
static void Main(string[] args)
{
StreamReader input_file = null;
StreamWriter output_file = null;

string in_file;
in_file = "test.txt";

string out_file;
out_file = "test.txt";

List<string> elements = new List<string>();
elements.Clear();

Header.Pullout_contents(in_file, elements);

Header.Show_contents(output_file, elements);

Header.Pushin_contents(out_file + ".out", elements);

if (input_file != null)
{
input_file.Close();
output_file.Close();
}
Console.ReadLine();
}
}
}


//Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;//to use StreamReader
namespace CreateDll
{
//public class Class1
//{
//}
public class Header
{
public static void Pullout_contents(string in_file, List<string> elements)
{
StreamReader input_file = new StreamReader(in_file.ToString());
elements.Clear();
string line = "";
for (int line_no = 1; (line = input_file.ReadLine()) != null; ++line_no)
{
elements.Add(line);
}
}

public static void Show_contents(StreamWriter Out, List<string> elements)
{
foreach (string iter in elements)
{
Console.WriteLine(iter);
}
}

public static void Pushin_contents(string out_file, List<string> elements)
{
StreamWriter output_file = new StreamWriter(out_file.ToString());
output_file.AutoFlush = true;
Console.SetOut(output_file);
Show_contents(output_file, elements);// using polymorphism
}
}
}


#csharp #cplusplus #tutorial