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: - <StructLayout(LayoutKind.Sequential, charset:=CharSet.Ansi)> _
-
Public Structure CONFIG_PARM
-
<MarshalAs(UnmanagedType.LPStr)> Public address1 As String
-
<MarshalAs(UnmanagedType.LPStr)> Public addrindex As String
-
<MarshalAs(UnmanagedType.LPStr)> Public cdrom As String
-
<MarshalAs(UnmanagedType.LPStr)> Public citystate As String
-
<MarshalAs(UnmanagedType.LPStr)> Public crossref As String
-
<MarshalAs(UnmanagedType.LPStr)> Public system As String
-
<MarshalAs(UnmanagedType.LPStr)> Public elot As String
-
<MarshalAs(UnmanagedType.LPStr)> Public elotindex As String
-
<MarshalAs(UnmanagedType.LPStr)> Public llkpath As String
-
<MarshalAs(UnmanagedType.LPStr)> Public ewspath As String
-
<MarshalAs(UnmanagedType.LPStr)> Public dpvpath As String
-
<MarshalAs(UnmanagedType.LPStr)> Public fnsnpath As String
-
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=124)> Public rsvd As String
-
End Structure
-
-
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
-
Public Structure Z4OPEN_PARM
-
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> Public rsvd1 As String
-
<MarshalAs(UnmanagedType.I4)> Public status As Integer
-
<MarshalAs(UnmanagedType.LPStr)> Public fname As String
-
<MarshalAs(UnmanagedType.Struct)> Public config As CONFIG_PARM
-
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Public ewsflag As String
-
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Public elotflag As String
-
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Public llkflag As String
-
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Public dpvflag As String
-
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> Public systemflag As String
-
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=511)> Public rsvd2 As String
-
End Structure
The Function declarations and the code used to call them are: - <DllImport("zip4_w32.dll", CharSet:=CharSet.Ansi)> _
-
Public Shared Function z4opencfgSTD(ByRef openparm As Z4OPEN_PARM) As Integer
-
End Function
-
-
...
-
-
Dim openparm As New Z4OPEN_PARM
-
Dim configparm As New CONFIG_PARM
-
-
Dim address1 As String = "e:\ams\ams_comm"
-
Dim addrindex As String = "e:\ams\ams_comm"
-
Dim citystate As String = "e:\ams\ams_comm"
-
Dim crossref As String = "e:\ams\ams_comm"
-
Dim fnsnpath As String = "e:\ams\ams_comm"
-
Dim elot As String = "e:\ams\ams_elot"
-
Dim elotindex As String = "e:\ams\ams_elot"
-
Dim llkpath As String = "e:\ams\lacslink"
-
Dim dpvpath As String = "e:\ams\ams_dpv"
-
Dim systempath As String = "e:\projects\wrapz4\wrapz4\debug"
-
Dim ewspath As String = ""
-
Dim rsvd_string As New StringBuilder
-
Dim rsvd As String = ""
-
rsvd.PadLeft(124)
-
Dim rsvd1 As String = ""
-
rsvd1.PadLeft(50)
-
Dim rsvd2 As String = ""
-
rsvd2.PadLeft(511)
-
Dim cdrom As String = ""
-
Dim fname As String = ""
-
-
configparm.address1 = address1
-
configparm.addrindex = addrindex
-
configparm.cdrom = cdrom
-
configparm.citystate = citystate
-
configparm.crossref = crossref
-
configparm.system = systempath
-
configparm.elot = elot
-
configparm.elotindex = elotindex
-
configparm.llkpath = llkpath
-
configparm.ewspath = ewspath
-
configparm.dpvpath = dpvpath
-
configparm.fnsnpath = fnsnpath
-
configparm.rsvd = rsvd
-
-
openparm.status = 2
-
openparm.rsvd1 = rsvd1
-
openparm.rsvd2 = rsvd2
-
openparm.elotflag = "Y"
-
openparm.llkflag = "Y"
-
openparm.ewsflag = "N"
-
openparm.dpvflag = "Y"
-
openparm.systemflag = "Y"
-
openparm.fname = fname
-
-
openparm.config = configparm
-
-
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.
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)
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.
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
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 ?
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
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."
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
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)"
Sign in to post your reply or Sign up for a free account.
Similar topics |
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)
|
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
|
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
|
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>");
|
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">
| |
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.
|
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?
|
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)
|
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
...
|
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) {
|
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: 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...
|
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: 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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |