473,785 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB.NET Accessing C DLL Problems

webroten
2 New Member
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 4896
Frinavale
9,735 Recognized Expert Moderator Expert
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
webroten
2 New Member
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 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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 New Member
Hi,
This is what I have changed.
<DllImport("zip 4_w32.dll", CallingConventi on:=CallingConv ention.StdCall, EntryPoint:="z4 opencfgSTD", ExactSpelling:= False), ReliabilityCont ract(Consistenc y.WillNotCorrup tState, Cer.None)> _
Public Shared Function z4opencfgSTD(<[In](), Out()> <MarshalAs(Unma nagedType.LPStr uct)> 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
1547
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 accessing COM objects that have been inserted in the script runtime by the msscript->AddObject function. Have anybody had similar problems? To be more specific the code: Monitor.Echo(logTxt, output)
9
3906
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 accssing attributes of the a subject given a particular semester. Can someone help me out with the syntax
1
4245
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: ct_connect(): network packet layer: internal net library error: Net-Library operation terminated due to disconnect
1
4054
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"; function ShowDetail( ) { top.frames.document.clear(); top.frames.document.write("<html><body>");
1
1596
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"/> </Course> <Course CourseCode="id2">
1
3962
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 class. The service is returning with the error code: -2147467259. I have not succeeded to find any additional information about the error. The "SoapFault" method seems to return only a bunch of question marks.
3
1511
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 some kind of security involved in accessing DCOM components? If yes, what kind? Is it configurable?
1
3140
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 elements and would like to seek a solution for this. I had looked through several articles for accessing programatically-created dynamic elements such as: 1)
7
7118
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 work with J2ME client a) c# web service works with c# WIndows Client (local & remote web service) b) c# web service works with J2ME client - when the Web Service is local c) c# web service fails with J2ME client - when the Web Service is remote ...
16
6798
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
10350
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, 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...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
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,...
0
9957
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 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...
0
6742
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();...
0
5386
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...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.