Handling C pointers within .NET

  • Thread starter Thread starter Albertas
  • Start date Start date
A

Albertas

Hello,

I'm building a program where i want to use function which is written
in C programming language. So I built .dll of it.
Functions parameters list looks this:

void function(char *input, char *output, int number_in, int number_out,
int *good);


In my C# project i have included it in following way:

[DllImport("Project1.dll")]
public unsafe static extern void function(char *input, char *output,
int number_in, int number_out, int *good);

So far I could not figure out myself how I can pass the parameters to
this function in C# environment. The second function parameter will
have the result values. So i need to extract them. Did I import my .dll
correct ? Could some one provide me with code example or explanation
how I could get it working?

Thank You.
 
Hi Albertas,

An easy way to import your unmanaged library so that it can be consumed by
managed code is to use the tlbimp program:

TlbImp program on MSDN:
http://msdn2.microsoft.com/en-us/library/tt0cf3sx.aspx

If you take a look at the result of the import with tlbimp you'll notice that
the generated code doesn't require pointers or unsafe code at all, so your
attempt to explicitly import a function from Project1.dll is overkill for C#
since the pointers aren't required.
 
but that assumes that the underlying type is a COM object.

what you want is

[DllImport("Project1.dll")]
public static extern void function(ref char input, ref char output, int
number_in, int number_out, ref int good);

Dave Sexton said:
Hi Albertas,

An easy way to import your unmanaged library so that it can be consumed by
managed code is to use the tlbimp program:

TlbImp program on MSDN:
http://msdn2.microsoft.com/en-us/library/tt0cf3sx.aspx

If you take a look at the result of the import with tlbimp you'll notice
that the generated code doesn't require pointers or unsafe code at all, so
your attempt to explicitly import a function from Project1.dll is overkill
for C# since the pointers aren't required.

--
Dave Sexton

Albertas said:
Hello,

I'm building a program where i want to use function which is written
in C programming language. So I built .dll of it.
Functions parameters list looks this:

void function(char *input, char *output, int number_in, int number_out,
int *good);


In my C# project i have included it in following way:

[DllImport("Project1.dll")]
public unsafe static extern void function(char *input, char *output,
int number_in, int number_out, int *good);

So far I could not figure out myself how I can pass the parameters to
this function in C# environment. The second function parameter will
have the result values. So i need to extract them. Did I import my .dll
correct ? Could some one provide me with code example or explanation
how I could get it working?

Thank You.
 
Hi,

You are correct, it was an assumption on my part that Project1 was written for
COM. P-Invoke doesn't require COM, but if COM is being used than TlbImp would
be a better choice, IMO.

Thanks for pointing that out.

--
Dave Sexton

CuriousGeorge said:
but that assumes that the underlying type is a COM object.

what you want is

[DllImport("Project1.dll")]
public static extern void function(ref char input, ref char output, int
number_in, int number_out, ref int good);

Dave Sexton said:
Hi Albertas,

An easy way to import your unmanaged library so that it can be consumed by
managed code is to use the tlbimp program:

TlbImp program on MSDN:
http://msdn2.microsoft.com/en-us/library/tt0cf3sx.aspx

If you take a look at the result of the import with tlbimp you'll notice
that the generated code doesn't require pointers or unsafe code at all, so
your attempt to explicitly import a function from Project1.dll is overkill
for C# since the pointers aren't required.

--
Dave Sexton

Albertas said:
Hello,

I'm building a program where i want to use function which is written
in C programming language. So I built .dll of it.
Functions parameters list looks this:

void function(char *input, char *output, int number_in, int number_out,
int *good);


In my C# project i have included it in following way:

[DllImport("Project1.dll")]
public unsafe static extern void function(char *input, char *output,
int number_in, int number_out, int *good);

So far I could not figure out myself how I can pass the parameters to
this function in C# environment. The second function parameter will
have the result values. So i need to extract them. Did I import my .dll
correct ? Could some one provide me with code example or explanation
how I could get it working?

Thank You.
 
Thank you for fast response.
I was not able to use TlbImp, it said that I'm using invalid type
library.
The problem is that I have worked with C++ and unmanaged not too much,
and I don't feel very comfortable there. Now I need to test few
functions that I have source code in C. And I want to implement them to
use in .NET.

I have changed my code line with:

[DllImport("Project1.dll")]
public static extern void function(ref char input, ref char output, int

number_in, int number_out, ref int good);

And when i make a call to function like this:

char[] input = { '0', '1', '1', '1', '0', '0'};
char[] output = {'0', '1' };

int a = 0;

c17.funkcija(ref input[0], ref output[0], 5, 2, ref a);

I get an error: "Unable to find an entry point named 'function' in DLL
'Project1.dll'."
So, now my guess is that i build .dll library in wrong way. What i did
is opened Borland C++ Builder, opened DLL Wizard, pasted function code
without any include statements and built Project1.dll. Could someone
point me to way out of this problem?

Thank You


Dave said:
Hi Albertas,

An easy way to import your unmanaged library so that it can be consumed by
managed code is to use the tlbimp program:

TlbImp program on MSDN:
http://msdn2.microsoft.com/en-us/library/tt0cf3sx.aspx

If you take a look at the result of the import with tlbimp you'll notice that
the generated code doesn't require pointers or unsafe code at all, so your
attempt to explicitly import a function from Project1.dll is overkill for C#
since the pointers aren't required.

--
Dave Sexton

Albertas said:
Hello,

I'm building a program where i want to use function which is written
in C programming language. So I built .dll of it.
Functions parameters list looks this:

void function(char *input, char *output, int number_in, int number_out,
int *good);


In my C# project i have included it in following way:

[DllImport("Project1.dll")]
public unsafe static extern void function(char *input, char *output,
int number_in, int number_out, int *good);

So far I could not figure out myself how I can pass the parameters to
this function in C# environment. The second function parameter will
have the result values. So i need to extract them. Did I import my .dll
correct ? Could some one provide me with code example or explanation
how I could get it working?

Thank You.
 
Hi Albertas,

Is "function" the exact name of the function in Project1 that you're trying to
invoke?

Here's a reference that you might want to read:

Consuming Unmanaged Dll Functions on MSDN:
http://msdn2.microsoft.com/en-us/library/26thfadc.aspx

--
Dave Sexton

Albertas said:
Thank you for fast response.
I was not able to use TlbImp, it said that I'm using invalid type
library.
The problem is that I have worked with C++ and unmanaged not too much,
and I don't feel very comfortable there. Now I need to test few
functions that I have source code in C. And I want to implement them to
use in .NET.

I have changed my code line with:

[DllImport("Project1.dll")]
public static extern void function(ref char input, ref char output, int

number_in, int number_out, ref int good);

And when i make a call to function like this:

char[] input = { '0', '1', '1', '1', '0', '0'};
char[] output = {'0', '1' };

int a = 0;

c17.funkcija(ref input[0], ref output[0], 5, 2, ref a);

I get an error: "Unable to find an entry point named 'function' in DLL
'Project1.dll'."
So, now my guess is that i build .dll library in wrong way. What i did
is opened Borland C++ Builder, opened DLL Wizard, pasted function code
without any include statements and built Project1.dll. Could someone
point me to way out of this problem?

Thank You


Dave said:
Hi Albertas,

An easy way to import your unmanaged library so that it can be consumed by
managed code is to use the tlbimp program:

TlbImp program on MSDN:
http://msdn2.microsoft.com/en-us/library/tt0cf3sx.aspx

If you take a look at the result of the import with tlbimp you'll notice
that
the generated code doesn't require pointers or unsafe code at all, so your
attempt to explicitly import a function from Project1.dll is overkill for
C#
since the pointers aren't required.

--
Dave Sexton

Albertas said:
Hello,

I'm building a program where i want to use function which is written
in C programming language. So I built .dll of it.
Functions parameters list looks this:

void function(char *input, char *output, int number_in, int number_out,
int *good);


In my C# project i have included it in following way:

[DllImport("Project1.dll")]
public unsafe static extern void function(char *input, char *output,
int number_in, int number_out, int *good);

So far I could not figure out myself how I can pass the parameters to
this function in C# environment. The second function parameter will
have the result values. So i need to extract them. Did I import my .dll
correct ? Could some one provide me with code example or explanation
how I could get it working?

Thank You.
 
Back
Top