473,402 Members | 2,072 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,402 software developers and data experts.

VB.NET Accessing C DLL Problems

webroten
I've been working through trying to access a C DLL from VB.NET. I've read many online postings, but I'm still having problems. Now, my error is the "Attempted to read or write protected memory" error. Here's my Structure declaration:

Expand|Select|Wrap|Line Numbers
  1. <StructLayout(LayoutKind.Sequential, charset:=CharSet.Ansi)> _
  2. Public Structure CONFIG_PARM
  3.     <MarshalAs(UnmanagedType.LPStr)> Public address1 As String
  4.     <MarshalAs(UnmanagedType.LPStr)> Public addrindex As String
  5.     <MarshalAs(UnmanagedType.LPStr)> Public cdrom As String
  6.     <MarshalAs(UnmanagedType.LPStr)> Public citystate As String
  7.     <MarshalAs(UnmanagedType.LPStr)> Public crossref As String
  8.     <MarshalAs(UnmanagedType.LPStr)> Public system As String
  9.     <MarshalAs(UnmanagedType.LPStr)> Public elot As String
  10.     <MarshalAs(UnmanagedType.LPStr)> Public elotindex As String
  11.     <MarshalAs(UnmanagedType.LPStr)> Public llkpath As String
  12.     <MarshalAs(UnmanagedType.LPStr)> Public ewspath As String
  13.     <MarshalAs(UnmanagedType.LPStr)> Public dpvpath As String
  14.     <MarshalAs(UnmanagedType.LPStr)> Public fnsnpath As String
  15.     <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=124)> Public rsvd As String
  16. End Structure
  17.  
  18. <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
  19. Public Structure Z4OPEN_PARM
  20.     <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> Public rsvd1 As String
  21.     <MarshalAs(UnmanagedType.I4)> Public status As Integer
  22.     <MarshalAs(UnmanagedType.LPStr)> Public fname As String
  23.     <MarshalAs(UnmanagedType.Struct)> Public config As CONFIG_PARM
  24.     <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Public ewsflag As String
  25.     <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Public elotflag As String
  26.     <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Public llkflag As String
  27.     <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Public dpvflag As String
  28.     <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Public systemflag As String
  29.     <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=511)> Public rsvd2 As String
  30. End Structure
The Function declarations and the code used to call them are:

Expand|Select|Wrap|Line Numbers
  1.     <DllImport("zip4_w32.dll", CharSet:=CharSet.Ansi)> _
  2.     Public Shared Function z4opencfgSTD(ByRef openparm As Z4OPEN_PARM) As Integer
  3.     End Function
  4.  
  5. ...
  6.  
  7.     Dim openparm As New Z4OPEN_PARM
  8.     Dim configparm As New CONFIG_PARM
  9.  
  10.         Dim address1 As String = "e:\ams\ams_comm"
  11.         Dim addrindex As String = "e:\ams\ams_comm"
  12.         Dim citystate As String = "e:\ams\ams_comm"
  13.         Dim crossref As String = "e:\ams\ams_comm"
  14.         Dim fnsnpath As String = "e:\ams\ams_comm"
  15.         Dim elot As String = "e:\ams\ams_elot"
  16.         Dim elotindex As String = "e:\ams\ams_elot"
  17.         Dim llkpath As String = "e:\ams\lacslink"
  18.         Dim dpvpath As String = "e:\ams\ams_dpv"
  19.         Dim systempath As String = "e:\projects\wrapz4\wrapz4\debug"
  20.         Dim ewspath As String = ""
  21.         Dim rsvd_string As New StringBuilder
  22.         Dim rsvd As String = ""
  23.         rsvd.PadLeft(124)
  24.         Dim rsvd1 As String = ""
  25.         rsvd1.PadLeft(50)
  26.         Dim rsvd2 As String = ""
  27.         rsvd2.PadLeft(511)
  28.         Dim cdrom As String = ""
  29.         Dim fname As String = ""
  30.  
  31.         configparm.address1 = address1
  32.         configparm.addrindex = addrindex
  33.         configparm.cdrom = cdrom
  34.         configparm.citystate = citystate
  35.         configparm.crossref = crossref
  36.         configparm.system = systempath
  37.         configparm.elot = elot
  38.         configparm.elotindex = elotindex
  39.         configparm.llkpath = llkpath
  40.         configparm.ewspath = ewspath
  41.         configparm.dpvpath = dpvpath
  42.         configparm.fnsnpath = fnsnpath
  43.         configparm.rsvd = rsvd
  44.  
  45.         openparm.status = 2
  46.         openparm.rsvd1 = rsvd1
  47.         openparm.rsvd2 = rsvd2
  48.         openparm.elotflag = "Y"
  49.         openparm.llkflag = "Y"
  50.         openparm.ewsflag = "N"
  51.         openparm.dpvflag = "Y"
  52.         openparm.systemflag = "Y"
  53.         openparm.fname = fname
  54.  
  55.         openparm.config = configparm
  56.  
  57.         Connection.Text = z4functions.z4opencfgSTD(openparm)
Why would I be getting the memory error, "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."?

Thanks for your help.
Nov 21 '07 #1
8 4879
Frinavale
9,735 Expert Mod 8TB
I've been working through trying to access a C DLL from VB.NET. I've read many online postings, but I'm still having problems. Now, my error is the "Attempted to read or write protected memory" error. Here's my Structure declaration:

...

Why would I be getting the memory error, "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."?

Thanks for your help.
I just encountered this type of error today.
What's happening is your C DLL is not properly using the memory allocated to it.

In my case I had to increase the size of the array that I passed to a method because this array was not large enough to hold the data that the function was manipulating. You would think that the developers of the DLL would have managed the memory correctly but this isn't the case.

I haven't really taken a close look at your code that you've posted...but I recommend going through and making sure that any variables declared in your .NET program are allocating enough memory for the C variables....

Especially Strings! C code is notorious for not handling memory allocation for Strings correctly.

(In your case...you may be providing Strings that are too long for the C DLL to handle)
Nov 21 '07 #2
That corrected the problem - thanks! I'm still having problems with the DLL reading my structure, but I don't think it's related to this issue. It could be something else with the marshalling or something.
Nov 27 '07 #3
Frinavale
9,735 Expert Mod 8TB
That corrected the problem - thanks! I'm still having problems with the DLL reading my structure, but I don't think it's related to this issue. It could be something else with the marshalling or something.
I'm glad you solved the problem!
If you need help with your other issue start a new post.

Congratz!

-Frinny
Nov 27 '07 #4
jm1576
3
I am working on the same exact project. Were you able to solve your issue ? I have just received the CD from USPS. Could you post the rest of the code that call the other Z4 functions ?
Jan 23 '08 #5
Frinavale
9,735 Expert Mod 8TB
I am working on the same exact project. Were you able to solve your issue ? I have just received the CD from USPS. Could you post the rest of the code that call the other Z4 functions ?
Hi Jm1576,

Webroten has only ever posted twice. This user is not very active.
Could you please post your exact error so that we might be able to help you?

-Frinny
Jan 23 '08 #6
jm1576
3
I am using the above code that was posted. This the error that I get

"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Jan 23 '08 #7
Frinavale
9,735 Expert Mod 8TB
I am using the above code that was posted. This the error that I get

"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
This is often a sign that the unmanaged code (the C code) is not using memory correctly. The previous person found that the problem was resolved when they allocated more memory for the C code to use...or else they shortened the strings that they were passing to the C code.

-Frinny
Jan 25 '08 #8
jm1576
3
Hi,
This is what I have changed.
<DllImport("zip4_w32.dll", CallingConvention:=CallingConvention.StdCall, EntryPoint:="z4opencfgSTD", ExactSpelling:=False), ReliabilityContract(Consistency.WillNotCorruptStat e, Cer.None)> _
Public Shared Function z4opencfgSTD(<[In](), Out()> <MarshalAs(UnmanagedType.LPStruct)> ByVal parm As Z4OPEN_PARM) As Integer
End Function

This is the error that I am getting now.
"Cannot marshal 'parameter #1': Invalid managed/unmanaged type combination (this value type must be paired with Struct)"
Jan 28 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Svenn-Ivar Svendsen | last post by:
I'm implementing a windows application with generic support for scripting, and I'm using the microsoft script control (msscript). For vbscript/jscript I'm happy, but with python I got problems...
9
by: ree | last post by:
Unlike arrays I am having problems accessing objects placed in a vector I have a vector of objects called Semesters Each Semester has its own vector of Subjects. I am having problems...
1
by: Amy Tseng | last post by:
Hi, I am having a problem accessing SQL Server 2000 via UNIX. I am accessing SQL Server 2000 from Solaris using Sybase Open Client (CT-Lib). Here is the error message: CT-LIBRARY error:...
1
by: Gregor | last post by:
I'm having problems accessing a global array variable from within a function. I have something like this: ------------------------------------ var detail = new Array( 20 ); detail = "hello"; ...
1
by: Jesper Stocholm | last post by:
I have som XML like this: <root> <Course CourseCode="id1"> <Teacher Name="Some name"/> <Title Titlde="Dansk Titel 1"/> <Title Title="English Title 1"/> <Location Place="Some place"/>...
1
by: Eirik Brattbakk | last post by:
Hi I have some problems accessing a soap service made in c# using an ATL/MFC client over SSL. I have tried both CSoapMSXMLInetClient and CSoapWininetClient as template arguments with my stub...
3
by: Oleg Skopincevs | last post by:
Hi folks, I am stuck on a problem of creating an object via DCOM from a webservice component. Code: Dim sl As LogonSrv.SysLogon = CreateObject("LogonSrv.SysLogon", LogonServer) Is there...
1
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created...
7
by: David Laub | last post by:
I've also posted this issue to a Sun/java formum, but since it appears to be an integration issue, this may be the better place to posr: I have written a dot net/c# Web Services doesn't fully...
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.