I'm trying to call a DLL function that receives as parameter a user-defined
structure created by the company that made the dll.
When I call the function from my main form, I call
dllCalls.addVer ("string1", "string2") -this is how I created the method in
the class. Now, in this static method that uses DllImport to read the dll,
I need to move "string1" and "string2" into a structure called PARMS_DIRS
since the only way the dll function can be executed is if I call it with a
parm of type PARMS_DIRS. In the example given by the company, they use this
code:
#include <stdlib.h>
#include <parms.h> // header file with PARMS_DIRS info
PARMS_DIRS parm; //the proprietary structure
void main(void)
{
...
memset(&parm,0, sizeof(parm));
strcpy(parm.typ e1, "100239"); //first string to be moved into struct
strcpy(parm.typ e2, "John Smith"); //second string to be moved into
struct
verName(&parm); //verName is the function in the DLL
...
}
After the function is executed, it fills the struct with other additional
info (parm.result1, parm.result2) which I need to process the employee.
How can I have access to this structure from within my C# application?
Thanks. 4 2784
Hi Angle,
It depends on how the structure PARMS_DIRS is declared.
You have to declare managed version of this structure.
If the structure has those two strings as in-lined strings in the structure
you should use MarshalAsAttrib ute with unmanaged type ByValTStr and properly
set SizeConst property.
If the structure has the strings as pointers you should use StringBuilder
type in your managed structure since you want the strings to be changed
--
HTH
B\rgds
100
"Angel" <none> wrote in message news:es******** *****@tk2msftng p13.phx.gbl... I'm trying to call a DLL function that receives as parameter a
user-defined structure created by the company that made the dll. When I call the function from my main form, I call dllCalls.addVer ("string1", "string2") -this is how I created the method in the class. Now, in this static method that uses DllImport to read the
dll, I need to move "string1" and "string2" into a structure called PARMS_DIRS since the only way the dll function can be executed is if I call it with a parm of type PARMS_DIRS. In the example given by the company, they use
this code:
#include <stdlib.h> #include <parms.h> // header file with PARMS_DIRS info PARMS_DIRS parm; //the proprietary structure
void main(void) { ... memset(&parm,0, sizeof(parm)); strcpy(parm.typ e1, "100239"); //first string to be moved into
struct strcpy(parm.typ e2, "John Smith"); //second string to be moved into struct verName(&parm); //verName is the function in the DLL ... }
After the function is executed, it fills the struct with other additional info (parm.result1, parm.result2) which I need to process the employee.
How can I have access to this structure from within my C# application?
Thanks.
Thanks for your response.
I tried doing that but I received a "Could not load ... from assembly
verName because the method verName has no RVA."
The original C struct looked like this:
typedef struct
{
char type1[1];
char type2[1];
struct
{
char result1[1];
char result2[1];
} foot;
char rsvd4[1];
} ZIP4_PARM;
and I converted it in Parms.cs as:
public struct ZIP4_PARM
{
char type1;
char type2;
struct foot
{
char result1;
char result2;
}
char rsvd;
]
What can I do?
Thanks.
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:uk******** *****@TK2MSFTNG P11.phx.gbl... Hi Angle, It depends on how the structure PARMS_DIRS is declared. You have to declare managed version of this structure.
If the structure has those two strings as in-lined strings in the
structure you should use MarshalAsAttrib ute with unmanaged type ByValTStr and
properly set SizeConst property.
If the structure has the strings as pointers you should use StringBuilder type in your managed structure since you want the strings to be changed
-- HTH B\rgds 100
"Angel" <none> wrote in message
news:es******** *****@tk2msftng p13.phx.gbl... I'm trying to call a DLL function that receives as parameter a
user-defined structure created by the company that made the dll. When I call the function from my main form, I call dllCalls.addVer ("string1", "string2") -this is how I created the method
in the class. Now, in this static method that uses DllImport to read the dll, I need to move "string1" and "string2" into a structure called
PARMS_DIRS since the only way the dll function can be executed is if I call it with
a parm of type PARMS_DIRS. In the example given by the company, they use this code:
#include <stdlib.h> #include <parms.h> // header file with PARMS_DIRS info PARMS_DIRS parm; //the proprietary structure
void main(void) { ... memset(&parm,0, sizeof(parm)); strcpy(parm.typ e1, "100239"); //first string to be moved into struct strcpy(parm.typ e2, "John Smith"); //second string to be moved
into struct verName(&parm); //verName is the function in the DLL ... }
After the function is executed, it fills the struct with other
additional info (parm.result1, parm.result2) which I need to process the employee.
How can I have access to this structure from within my C# application?
Thanks.
Hi Angel,
1. If the members are arrays of 1 character in order to have them properly
marshaled you should declare them as strings.
2. Even though in the original *result1* and *result2* are declared in a
nested structure you cannot do that in c#.
So, IMHO the corect declaration should be something like
[ StructLayout( LayoutKind.Sequ ential )]
struct ZIP4_PARM
{
[ MarshalAs( UnmanagedType.B yValTStr, SizeConst=1)]
public string type1;
[ MarshalAs( UnmanagedType.B yValTStr, SizeConst=1)]
public string type2;
[ MarshalAs( UnmanagedType.B yValTStr, SizeConst=1)]
public string result1;
[ MarshalAs( UnmanagedType.B yValTStr, SizeConst=1)]
public string result2;
[ MarshalAs( UnmanagedType.B yValTStr, SizeConst=1)]
public string rsvd;
}
However, there are other things you should take care of.
You might need to set the CharSet property of StructLayout attribute to
control whether the characters are ASCII or UNICODE. If the methods that you
call accepts pointer to the structure you may need to declare it as a class
instead of structure.
--
HTH
B\rgds
100
"Angel" <none> wrote in message
news:OD******** ******@TK2MSFTN GP10.phx.gbl... Thanks for your response. I tried doing that but I received a "Could not load ... from assembly verName because the method verName has no RVA."
The original C struct looked like this: typedef struct { char type1[1]; char type2[1]; struct { char result1[1]; char result2[1]; } foot; char rsvd4[1]; } ZIP4_PARM;
and I converted it in Parms.cs as:
public struct ZIP4_PARM { char type1; char type2; struct foot { char result1; char result2; } char rsvd; ]
What can I do?
Thanks. "Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message news:uk******** *****@TK2MSFTNG P11.phx.gbl... Hi Angle, It depends on how the structure PARMS_DIRS is declared. You have to declare managed version of this structure.
If the structure has those two strings as in-lined strings in the structure you should use MarshalAsAttrib ute with unmanaged type ByValTStr and properly set SizeConst property.
If the structure has the strings as pointers you should use
StringBuilder type in your managed structure since you want the strings to be changed
-- HTH B\rgds 100
"Angel" <none> wrote in message news:es******** *****@tk2msftng p13.phx.gbl... I'm trying to call a DLL function that receives as parameter a
user-defined structure created by the company that made the dll. When I call the function from my main form, I call dllCalls.addVer ("string1", "string2") -this is how I created the
method in the class. Now, in this static method that uses DllImport to read the dll, I need to move "string1" and "string2" into a structure called PARMS_DIRS since the only way the dll function can be executed is if I call it
with a parm of type PARMS_DIRS. In the example given by the company, they use this code:
#include <stdlib.h> #include <parms.h> // header file with PARMS_DIRS info PARMS_DIRS parm; //the proprietary structure
void main(void) { ... memset(&parm,0, sizeof(parm)); strcpy(parm.typ e1, "100239"); //first string to be moved into struct strcpy(parm.typ e2, "John Smith"); //second string to be moved into struct verName(&parm); //verName is the function in the DLL ... }
After the function is executed, it fills the struct with other additional info (parm.result1, parm.result2) which I need to process the
employee. How can I have access to this structure from within my C# application?
Thanks.
Hi Angel,
I think Stoitcho's reply is of much value. Is your problem resolved?
If you still have any concern, please feel free to post, we will help you.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: jarmopy |
last post by:
Hi,
I have made a service with C# and calling that service class from another C#
program with remoting.
(Referendes from the calling program)
That service class is configured so that garpage collection is not used in
this class.
(singleton class + override InitializeLifetimeService )
The service class uses C++ unmanaged function from dll (using DLLimport).
|
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 performs the query
is contained in a delegate function that I execute via a second thread. On 1
or 2 of the 600+ servers the query hangs. I've tried to use Thread.Join()
coupled with a Thread.Abort() but this does not kill the thread.
Based on...
|
by: Webdiyer |
last post by:
I want to integrate SecurID two-factor authentication system of the
RSASecurity.inc into our asp.net application,but I get into trouble when
trying to call the API functions of the ACE Agent,I got an error message
saying "Cannot marshal parameter #2: Invalid managed/unmanaged type
combination (this value type must be paired with Struct)" when calling
"AceGetPinParams(iHandle,ref sd_pin)" function,here's my test code:
private static...
|
by: Adam Clauss |
last post by:
I have an unmanaged C++ library that I need to use through C#. I created a
Managed C++ class library and through it wrote a wrapper class to call what
I need.
However, one of the methods (an Initialize function in the unmanaged
library) crashes everytime I call it. Initialize calls another function
'GetInstance' (a static member function of a class) which looks something
like:
CLibConfig& CLibConfig::GetInstance()
|
by: Bill Grigg |
last post by:
I have been successfully calling DLL's using VC++ 6.0 and also using VC++7.1
(.NET). I only mention this because I have never felt comfortable with the
process, but nonetheless it did work. Recently I started calling an unmanaged
DLL from a .NET app. It worked fine. Now I have the situation where I am
trying to call a function in one DLL from another DLL. Both are unmanaged and
are built with VC++7.1. In the calling DLL I set the property...
| |
by: slugster |
last post by:
Hi,
i originally posted this via another portal, but after giving it time
to propagate it still hasn't shown up. My apologies for the
multiposting.
This might be a very simple question, but i am still getting my head
round this stuff.
I have a mixed mode dll. In it i have a managed class with a function,
i want to call that function from a function in an unmanaged class.
|
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, System::EventArgs * e)
{
MyDLLInit(MyAppDisplayFunction);
}
|
by: Leftie |
last post by:
Folks,
I'm trying to call an unmanaged function from VB.NET and
keep getting "Object reference not set to an instance of
an object" error.
The code that i wrote can be found at:
http://www.nomorepasting.com/paste.php?pasteID=5973
The dll (if some kind soul has the time to compile and run
the code) can be found at:
|
by: Klaus |
last post by:
Hi,
I have an existing VC 6 MFC application which communicates asynchronly with
a VC 2005 managed code dll.
I use an unmanaged base class with virtual functions to access methods in
the MFC application.
Furthermore, I use a pointer to an unmanaged function to jump back into the
managed dll.
The managed part is basically a remoting enhancement which asynchronly
|
by: Markus Eßmayr |
last post by:
Hello!
I'm writing a .NET component that forwards calls to it's functions on to an
unmanaged DLL. This DLL is loaded dynamically using LoadLibrary and the
function pointers are retrieved using GetProcAddress.
The "references" to the unmanaged DLL are held inside my ref class.
See this example:
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |