B
Barry Shilmover
hi all,
I am trying to call a simple eVC++ DLL from my C# project... here is what I
have set up:
testdll2.cpp:
// testdll2.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "testdll2.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
TESTDLL2_API bool Compress2(char *src, char *dest)
{
bool retval(true);
strcpy(dest, "Hello");
return retval;
}
testdll2.h:
// The following ifdef block is the standard way of creating macros
which make exporting
// from a DLL simpler. All files within this DLL are compiled with the
TESTDLL2_EXPORTS
// symbol defined on the command line. this symbol should not be defined
on any project
// that uses this DLL. This way any other project whose source files
include this file see
// TESTDLL2_API functions as being imported from a DLL, wheras this DLL
sees symbols
// defined with this macro as being exported.
#ifdef TESTDLL2_EXPORTS
#define TESTDLL2_API __declspec(dllexport)
#else
#define TESTDLL2_API __declspec(dllimport)
#endif
extern TESTDLL2_API bool Compress2(char *src, char *dest);
C# code:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace dlltest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.MainMenu mainMenu2;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.MainMenu mainMenu1;
[DllImport("testdll2.dll")]
public static extern bool Compress2(string src, string dest);
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.mainMenu2 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 40);
this.textBox1.Size = new System.Drawing.Size(160, 25);
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(8, 112);
this.textBox2.Size = new System.Drawing.Size(160, 25);
this.textBox2.Text = "textBox2";
//
// mainMenu2
//
this.mainMenu2.MenuItems.Add(this.menuItem1);
//
// menuItem1
//
this.menuItem1.Text = "Encrypt";
this.menuItem1.Click += new
System.EventHandler(this.menuItem1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Size = new System.Drawing.Size(152, 16);
this.label1.Text = "Text In";
//
// label2
//
this.label2.Font = new System.Drawing.Font("Nina", 11F,
System.Drawing.FontStyle.Bold);
this.label2.Location = new System.Drawing.Point(8, 88);
this.label2.Size = new System.Drawing.Size(152, 22);
this.label2.Text = "Text Out";
//
// Form1
//
this.Controls.Add(this.textBox1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Menu = this.mainMenu2;
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new Form1());
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
string src;
string dest;
src = "";
dest = "12345";
Compress2(src, dest);
this.textBox2.Text = dest;
}
}
}
Any ideas?
Barry
I am trying to call a simple eVC++ DLL from my C# project... here is what I
have set up:
testdll2.cpp:
// testdll2.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "testdll2.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
TESTDLL2_API bool Compress2(char *src, char *dest)
{
bool retval(true);
strcpy(dest, "Hello");
return retval;
}
testdll2.h:
// The following ifdef block is the standard way of creating macros
which make exporting
// from a DLL simpler. All files within this DLL are compiled with the
TESTDLL2_EXPORTS
// symbol defined on the command line. this symbol should not be defined
on any project
// that uses this DLL. This way any other project whose source files
include this file see
// TESTDLL2_API functions as being imported from a DLL, wheras this DLL
sees symbols
// defined with this macro as being exported.
#ifdef TESTDLL2_EXPORTS
#define TESTDLL2_API __declspec(dllexport)
#else
#define TESTDLL2_API __declspec(dllimport)
#endif
extern TESTDLL2_API bool Compress2(char *src, char *dest);
C# code:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace dlltest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.MainMenu mainMenu2;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.MainMenu mainMenu1;
[DllImport("testdll2.dll")]
public static extern bool Compress2(string src, string dest);
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.mainMenu2 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 40);
this.textBox1.Size = new System.Drawing.Size(160, 25);
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(8, 112);
this.textBox2.Size = new System.Drawing.Size(160, 25);
this.textBox2.Text = "textBox2";
//
// mainMenu2
//
this.mainMenu2.MenuItems.Add(this.menuItem1);
//
// menuItem1
//
this.menuItem1.Text = "Encrypt";
this.menuItem1.Click += new
System.EventHandler(this.menuItem1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Size = new System.Drawing.Size(152, 16);
this.label1.Text = "Text In";
//
// label2
//
this.label2.Font = new System.Drawing.Font("Nina", 11F,
System.Drawing.FontStyle.Bold);
this.label2.Location = new System.Drawing.Point(8, 88);
this.label2.Size = new System.Drawing.Size(152, 22);
this.label2.Text = "Text Out";
//
// Form1
//
this.Controls.Add(this.textBox1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Menu = this.mainMenu2;
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new Form1());
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
string src;
string dest;
src = "";
dest = "12345";
Compress2(src, dest);
this.textBox2.Text = dest;
}
}
}
Any ideas?
Barry