PC Review


Reply
Thread Tools Rate Thread

calling a eVC++ Dll from a C# CF .Net project

 
 
Barry Shilmover
Guest
Posts: n/a
 
      4th Nov 2003
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


 
Reply With Quote
 
 
 
 
Paul G. Tobey [eMVP]
Guest
Posts: n/a
 
      4th Nov 2003
You'll want to put

extern 'C' {
}

around the function or else the parameter and return value information will
'decorate' the name as it is exported from the DLL and you'll have to put
something other than the plain vanilla name in the p/invoke declaration.

Paul T.

"Barry Shilmover" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Barry Shilmover
Guest
Posts: n/a
 
      4th Nov 2003
I think I did that... no luck...

I have tried putting in an EntryPoint, but no luck there, either.

Barry

"Paul G. Tobey [eMVP]" <ptobey_no_spam@instrument_no_spam.com> wrote in
message news:(E-Mail Removed)...
> You'll want to put
>
> extern 'C' {
> }
>
> around the function or else the parameter and return value information

will
> 'decorate' the name as it is exported from the DLL and you'll have to put
> something other than the plain vanilla name in the p/invoke declaration.
>
> Paul T.
>
> "Barry Shilmover" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
Paul G. Tobey [eMVP]
Guest
Posts: n/a
 
      4th Nov 2003
ZIP up the code and send it to the newsgroup. Oh, and *tell us what's
happening*. I'm assuming that you're getting a missing method exception
when you try to do the p/invoke, but you haven't said that.

Paul T.

"Barry Shilmover" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> I think I did that... no luck...
>
> I have tried putting in an EntryPoint, but no luck there, either.
>
> Barry
>
> "Paul G. Tobey [eMVP]" <ptobey_no_spam@instrument_no_spam.com> wrote in
> message news:(E-Mail Removed)...
> > You'll want to put
> >
> > extern 'C' {
> > }
> >
> > around the function or else the parameter and return value information

> will
> > 'decorate' the name as it is exported from the DLL and you'll have to

put
> > something other than the plain vanilla name in the p/invoke declaration.
> >
> > Paul T.
> >
> > "Barry Shilmover" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Alex Feinman [MVP]
Guest
Posts: n/a
 
      4th Nov 2003
You cannot pass a string as a receive buffer to the P/Invoked function. You
also cannot use char (as opposite to wchar_t) with strin parameters. There
are several things you need to do:
1) Replace the second parameter wirth StringBuilder class
2) Add parameter that would hold the maximum buffer length and in the
unmanaged code make sure you use wcscpyn so that you do not overwrite the
buffer.
3) Replace char* with LPWCSTR and LPWSTR - you are dealing with Unicode
strings in Windows CE

[DllImport("testdll2.dll")]
public static extern bool Compress2(string src, StringBuilder dest);

int cb = 256;
StringBuilder sb = new StringBuilder(cb);
Compress2("Hello", sb, cb);
string sOut = sb.ToString();

I suspect that what you actually want is binary data, not strings. In this
case change the unmanaged code as this:

TESTDLL2_API bool Compress2(char *src, int cbsrc, char *dest, int cbdst)
{
bool retval(true);

CopyMemory(dest, "Hello", min(cbsrc, cbdst));

return retval;
}

[DllImport("testdll2.dll")]
public static extern bool Compress2(byte[]src, int cbsrc, byte[]dest, int
cbdst);

int cbdst = 256;
byte[] buffer = new byte[cbdst];
string strIn = "Hello";
byte[] arrIn = Encoding.Unicode.GetBytes(strIn);
Compress2(arrIn, arrIn.Length, buffer, cbdst);

"Barry Shilmover" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Brian Scott
Guest
Posts: n/a
 
      5th Nov 2003
Barry,

you may have the same problem I had,

Are you building for Pocket PC 2002 and using EVC++ 4.0 ? If so then the SDK
is not compatible, any dlls you build will result in a
MissingMEthodException. Try using EVC++ 3.0 if this is the case.

Regards,

Brian.

"Barry Shilmover" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> I think I did that... no luck...
>
> I have tried putting in an EntryPoint, but no luck there, either.
>
> Barry
>
> "Paul G. Tobey [eMVP]" <ptobey_no_spam@instrument_no_spam.com> wrote in
> message news:(E-Mail Removed)...
> > You'll want to put
> >
> > extern 'C' {
> > }
> >
> > around the function or else the parameter and return value information

> will
> > 'decorate' the name as it is exported from the DLL and you'll have to

put
> > something other than the plain vanilla name in the p/invoke declaration.
> >
> > Paul T.
> >
> > "Barry Shilmover" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a form in second project John Microsoft VB .NET 6 11th Apr 2008 12:17 AM
Calling procedures of a different project =?Utf-8?B?Sm9yZ2UgRGUgbGEgUm9zYQ==?= Microsoft Excel Programming 4 23rd Aug 2007 01:50 AM
calling vb.net form from vb6 project =?Utf-8?B?c2dzcGVuY2VyNTA=?= Microsoft VB .NET 0 22nd Jun 2004 10:45 PM
Calling VB Script from C# project. =?Utf-8?B?Sm9zdWE=?= Microsoft C# .NET 1 31st Mar 2004 10:25 PM
calling c++ function from a c# project fotis Microsoft C# .NET 1 26th Dec 2003 10:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:15 PM.