473,409 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Help With Calling Unmanaged C Code

I have a Windows DLL written in C that I need to call in VB code. The
function has two parameters and must modify the character array for
just one of the parms. A simplified example of C code I wish to call
from test.dll is as follows:

__declspec(dllexport) void CopyString (char *in, char *out)
{
strcpy_s(out, strlen(in), in);
return;
}

My question is how do I call this from VB? Will I have to use an
IntPtr, a Char array, or something else? I know I must allocate memory
for the out parameter, but I would prefer to do this in my managed VB
code.

Will my declaration look like the following or something else?:

<System.Runtime.InteropServices.DllImport("test.dl l")_
Private Shared Sub CopyString(ByVal in As Char(), _
ByVal out As Char())
End Sub

TIA
Jun 27 '08 #1
3 1401
On 2008-05-24, ja***@onepost.net <ja***@onepost.netwrote:
I have a Windows DLL written in C that I need to call in VB code. The
function has two parameters and must modify the character array for
just one of the parms. A simplified example of C code I wish to call
from test.dll is as follows:

__declspec(dllexport) void CopyString (char *in, char *out)
{
strcpy_s(out, strlen(in), in);
return;
}

My question is how do I call this from VB? Will I have to use an
IntPtr, a Char array, or something else? I know I must allocate memory
for the out parameter, but I would prefer to do this in my managed VB
code.

Will my declaration look like the following or something else?:

<System.Runtime.InteropServices.DllImport("test.d ll")_
Private Shared Sub CopyString(ByVal in As Char(), _
ByVal out As Char())
End Sub

TIA
Well... Assuming that this is a __stdcall function - then, it would
look something like:

Declare Ansi Sub CopyString Lib "test.dll" (ByVal in As String, ByVal out As System.Text.StringBuilder)

HTH

--
Tom Shelton
Jun 27 '08 #2
On Sat, 24 May 2008 09:33:19 -0700, Tom Shelton
<to*********@YOUKNOWTHEDRILLcomcast.netwrote:
>Well... Assuming that this is a __stdcall function - then, it would
look something like:

Declare Ansi Sub CopyString Lib "test.dll" (ByVal in As String, ByVal out As System.Text.StringBuilder)

HTH
It is doing a bit more than making standard function calls. The intent
behind my example was to show the parms.

I was not setting the character set though, and I modified my
declaration as follows:

<System.Runtime.InteropServices.DllImport("test.dl l",
CharSet:=CharSet.Ansi)_
Private Shared Sub CopyString(ByVal sIn As String, _
ByVal sOut StringBuilder)
End Sub

The issue I run into is that when this function is called, I receive a
generic error as follows:

Test.vshost.exe has encountered a problem and needs to close. We are
sorry for the inconvenience.

I'm not receiving anything useful in indicating what the issue is.
Jun 27 '08 #3
On May 24, 2:00*pm, ja...@onepost.net wrote:
On Sat, 24 May 2008 09:33:19 -0700, Tom Shelton

<tom_shel...@YOUKNOWTHEDRILLcomcast.netwrote:
Well... *Assuming that this is a __stdcall function - then, it would
look something like:
Declare Ansi Sub CopyString Lib "test.dll" (ByVal in As String, ByVal outAs System.Text.StringBuilder)
HTH

It is doing a bit more than making standard function calls. The intent
behind my example was to show the parms.

I was not setting the character set though, and I modified my
declaration as follows:

<System.Runtime.InteropServices.DllImport("test.dl l",
CharSet:=CharSet.Ansi)_
Private Shared Sub CopyString(ByVal sIn As String, _
* * * * * * * * * * * * * * * ByVal sOut StringBuilder)
End Sub

The issue I run into is that when this function is called, I receive a
generic error as follows:

Test.vshost.exe has encountered a problem and needs to close. We are
sorry for the inconvenience.

I'm not receiving anything useful in indicating what the issue i
Well... You need to allocate the StringBuilder first.
Dim sOut As New StringBuilder (sIn.Length)

An alternative, and maybe better thinking about it, is to pass the
second parameter as an IntPtr and then use he Marshal.PtrToStringAnsi
function to get back to a managed string.

--
Tom Shelton
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: john | last post by:
Here is the short story of what i'm trying to do. I have a 4 sided case labeling printer setting out on one of our production lines. Now then i have a vb.net application that sends data to this...
15
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
0
by: sklett | last post by:
I'm having a really hard time with wrapping an unmanaged class with a managed class, then calling that managed class from my C# code. I will show you the three pieces, then explain: --------...
4
by: VicVic | last post by:
Dear Great Experts, I have a problem and need your help! We have a Flat DLL built in C++ (CCC), and on top of it, we built another DLL built in C# (SSS), which has .Net Assembly. CCC was...
1
by: Jesse McGrew | last post by:
Hi all, I'm trying to make a plugin DLL for a third-party application, using VC++ .NET 2003. This DLL acts as a bridge between the C++ plugin API of the application, and my actual plugin code...
1
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
3
by: Tyler | last post by:
Can someone help by explaining why the following class will not compile (VS2005), and what can be done to pass the function pointer to the "C" method? When I compile the following program, I get...
1
by: MC-Advantica | last post by:
Does anyone have a simple "Hello World" like application that demonstrates unmanaged C++ calling managed C++ developed in VS2005? I'm confused by many posts as they discuss managed extensions from...
9
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for...
22
by: SQACSharp | last post by:
I'm trying to get the control name of an editbox in another window. The following code set the value "MyPassword" in the password EditBox but it fail to return the control name of the EditBox. ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.