Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Win API / April 2008



Tip: Looking for answers? Try searching our database.

CopyMemory within a callback

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JamesJ - 25 Apr 2008 11:18 GMT
Hi guys,

I need to call the CopyMemory function in VB6 from within a callback, all
works well within the VB Dev Environment, but causes a crash once compiled.

I have spent a few days researching this problem on the net and it looks to
be related to the fact that calling a function from a declare causes the
vbaSetSystemError to be set, which then subsequently causes the callback to
fail.

I have seen suggestions that using a Type Library could resolve the problem,
however when using the TLB file to define CopyMemory, I receive a cannot
'Read' memory error, and switching back to using the Declare within a module
I receive a cannot 'Write' memory error. (Please note that these errors only
occur in the compiled exe, and within the Vb Environment everything works
great.

I'd be really grateful for any your suggestions you may have.

Regards,
James
Dean Earley - 25 Apr 2008 11:37 GMT
> Hi guys,
>
[quoted text clipped - 12 lines]
> occur in the compiled exe, and within the Vb Environment everything works
> great.

Yes, you do need to use a typelib if the callback is in another thread
(what can do safely is really limited)
I expect you are getting errors down to difference in ByVal and ByRef
and param types.

The params in my typelib are:
pDst    [in] void*
pSrc    [in] void*
ByteLen    [in] long

Signature

Dean Earley (dean.earley@icode.co.uk)
i-Catcher Development Team

iCode Systems

JamesJ - 25 Apr 2008 16:23 GMT
Hi Dean,

Thanks for your suggested typlib changes, I tried them however my code still
unfortunately crashed when compiled with a memory read error.

I suspect you are right about problems with byref/byval etc, I've included
below a snippet of the code, hopefully you will be able to spot something?

' The following are present in a module.

Public Type EVENT_GENERIC_CONTAINER
   ContainerLength As Long
   ContainerType As Integer
   Code As Integer
   TransactionID As Long
   Parameter As Long
End Type

Private pevent As EVENT_GENERIC_CONTAINER

Public Sub EventCallback(CameraHandle As Long, ByVal Context As Long, ByVal
EventVoid As Long)
  ' any attempt to run RtlMoveMemory fails when compiled!
  RtlMoveMemory pevent, ByVal EventVoid, 16
  ' rest of code here
End Sub

Regards,
James

> > Hi guys,
> >
[quoted text clipped - 22 lines]
> pSrc    [in] void*
> ByteLen    [in] long
Dean Earley - 28 Apr 2008 09:46 GMT
> Hi Dean,
>
[quoted text clipped - 22 lines]
>    ' rest of code here
> End Sub

It more depends what the original CopyMemory declare looking like
I used:
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
(ByRef pDst As Any, ByRef pSrc As Any, ByVal ByteLen As Long)

Signature

Dean Earley (dean.earley@icode.co.uk)
i-Catcher Development Team

iCode Systems

JamesJ - 28 Apr 2008 10:13 GMT
Hi Dean,
How do you mean that it depends on the original copymemory declare? When I
used your suggested Typelib parameters, I can successfully use CopyMemory in
the Vb Environment, however I'm seeing a cannot 'read' memory error once
compiled.
As everything is working fine in the VB Dev Environment doesn't that mean
that I have already got the correct by ref or by val settings?
Regards,
James

> > Hi Dean,
> >
[quoted text clipped - 27 lines]
> Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
> (ByRef pDst As Any, ByRef pSrc As Any, ByVal ByteLen As Long)
Dean Earley - 28 Apr 2008 11:21 GMT
> Hi Dean,
> How do you mean that it depends on the original copymemory declare? When I
[quoted text clipped - 3 lines]
> As everything is working fine in the VB Dev Environment doesn't that mean
> that I have already got the correct by ref or by val settings?

Are you sure it is the copymemory itself that is causing the crash?

What you can call from another thread is VERY limited (no declares, no
com objects, no other classes, etc..)

When running in the IDE, VB marshals it all to the same thread so all
the problems "disappear"

Signature

Dean Earley (dean.earley@icode.co.uk)
i-Catcher Development Team

iCode Systems

JamesJ - 28 Apr 2008 12:15 GMT
Hi Dean,

Many thanks, yes it looks like I've been so caught up in trying to get the
CopyMemory working that I never noticed that other, more innocent operations
(like form1.print "Callback Triggerd") where actually causing the crash.

This leads me to one last question, if i may, how would one perform an
action such as form1.print "Event Triggered" from within the callback? Would
utilizing CreateThread from within the callback be the solution?

Regards,
James

> > Hi Dean,
> > How do you mean that it depends on the original copymemory declare? When I
[quoted text clipped - 11 lines]
> When running in the IDE, VB marshals it all to the same thread so all
> the problems "disappear"
Dean Earley - 28 Apr 2008 12:47 GMT
> Hi Dean,
>
[quoted text clipped - 5 lines]
> action such as form1.print "Event Triggered" from within the callback? Would
> utilizing CreateThread from within the callback be the solution?

No, as it is already in another thread, effectively isolating it from
the rest of the app.

Perhaps the simplest method is to use Send/PostMessage (using a variable
containing the forms hWnd) and subclass that to receive the message.

Signature

Dean Earley (dean.earley@icode.co.uk)
i-Catcher Development Team

iCode Systems

Sinna - 28 Apr 2008 10:14 GMT
> Hi guys,
>
[quoted text clipped - 17 lines]
> Regards,
> James

You'll have to take a closer look to your CopyMemory call.
Is it really a CopyMemory call or a MoveMemory call?
Also check if the parameters are passed correctly.
For me it seems like the IDE is more relax about the way the parameters
are passed in, once compiled it must be correctly, otherwise the app
crashes.
I've made the same mistake before, but depending on the situation you
have to make adjustments to the call.

Sinna
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.