www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Cpw Binding - Error: Access Violation

reply Trevor Parscal <Trevor_member pathlink.com> writes:
I just finished binding a windowing library, Cpw (http://www.mathies.com/cpw/),
to D, in a style simular to Derelict, meaning I use a loader to connect function
pointers to the functions in a DLL that was loaded into memory dynamicly.

The library works, sort of. I can get it to perform initialization and it will
create a window, however once it has to use the callbacks, if they use the
pointer to the Cpw context for anything, i get an access violation.

I will clarify.

extern(C)
{
void windowCallback(pCpw cpw, uint winid, bit flag)
{
if(!flag)
{
// Close the Window (! This is where it crashes !)
cpwDestroyWindow(cpw, winid);
}
}
}

int main(char[][] args)
{
// Load the DLL
cpwLoad();

// Create and Initialize a Cpw Context
pCpw cpw;
cpwInitContext(&cpw)

// Create a Window
cpwCreateWindowEx(cpw, "Basic Template", 200, 200, 640, 480);

// Set Callback
cpwCreateCallback(cpw, cast(CpwCreateCallback)&windowCallback);

// Run
cpwMainLoop(cpw);
cpwFreeContext(&cpw);
return 0;
}

So when you start a Cpw program, you create a pCpw, which is a pointer to a Cpw
context struct. Than you initialize it. You can create a window, and than set a
callback for different events of that window.

My problem is when the callback gets called, the pointer that Cpw is sending to
the function to identify the conext is for some reason not accessable to the
callback function, thus I get an Access Violation error.

If I don't make the callback functions in extern(C), I get an access violation
whenever Cpw tries to call the callback functions. If I don't specify a
callback, everything works fine, cause Cpw has defaults, that I guess do have
access to the context pointer, unlike mine.

I have tried defining the cpw pointer in the extern(C) area, but it makes no
difference.

Why do my callbacks not have access to the context pointer?

Thanks in advance for any and all help!

Thanks,
Trevor Parscal
www.trevorparscal.com
trevorparscal hotmail.com
May 27 2005
next sibling parent reply John Reimer <brk_6502 yahoo.com> writes:
Trevor Parscal wrote:
 I just finished binding a windowing library, Cpw (http://www.mathies.com/cpw/),
 to D, in a style simular to Derelict, meaning I use a loader to connect
function
 pointers to the functions in a DLL that was loaded into memory dynamicly.
 
 The library works, sort of. I can get it to perform initialization and it will
 create a window, however once it has to use the callbacks, if they use the
 pointer to the Cpw context for anything, i get an access violation.
 
 I will clarify.
 
 extern(C)
 {
 void windowCallback(pCpw cpw, uint winid, bit flag)
 {
 if(!flag)
 {
 // Close the Window (! This is where it crashes !)
 cpwDestroyWindow(cpw, winid);
 }
 }
 }
 
 int main(char[][] args)
 {
 // Load the DLL
 cpwLoad();
 
 // Create and Initialize a Cpw Context
 pCpw cpw;
 cpwInitContext(&cpw)
 
 // Create a Window
 cpwCreateWindowEx(cpw, "Basic Template", 200, 200, 640, 480);
 
 // Set Callback
 cpwCreateCallback(cpw, cast(CpwCreateCallback)&windowCallback);
 
 // Run
 cpwMainLoop(cpw);
 cpwFreeContext(&cpw);
 return 0;
 }
 
 So when you start a Cpw program, you create a pCpw, which is a pointer to a Cpw
 context struct. Than you initialize it. You can create a window, and than set a
 callback for different events of that window.
 
 My problem is when the callback gets called, the pointer that Cpw is sending to
 the function to identify the conext is for some reason not accessable to the
 callback function, thus I get an Access Violation error.
 
 If I don't make the callback functions in extern(C), I get an access violation
 whenever Cpw tries to call the callback functions. If I don't specify a
 callback, everything works fine, cause Cpw has defaults, that I guess do have
 access to the context pointer, unlike mine.
 
 I have tried defining the cpw pointer in the extern(C) area, but it makes no
 difference.
 
 Why do my callbacks not have access to the context pointer?
 
 Thanks in advance for any and all help!
 
 Thanks,
 Trevor Parscal
 www.trevorparscal.com
 trevorparscal hotmail.com

It could be a matter of calling convention. This looks slightly confusing, but maybe try making your callback extern(Windows) and see what happens (I assume you are working with windows). Hopefully this is /not/ the case because that would mean using Cpw is not perfectly cross-platform. It should really work with extern(C) as you demonstrated. I've never played with Cpw before, but I pleasantly surprised at its feature set. It looks like a useful little toolkit. -JJR
May 27 2005
parent John Reimer <brk_6502 yahoo.com> writes:
 It could be a matter of calling convention.  This looks slightly 
 confusing, but maybe try making your callback extern(Windows) and see 
 what happens (I assume you are working with windows).
 
 Hopefully this is /not/ the case because that would mean using Cpw is 
 not perfectly cross-platform.  It should really work with extern(C) as 
 you demonstrated.
 
 I've never played with Cpw before, but I pleasantly surprised at its 
 feature set.  It looks like a useful little toolkit.
 
 -JJR

Actually, on closer inspection of the Cpw library source, extern(Windows) doesn't appear to be the solution. Scratch that idea. -JJR
May 27 2005
prev sibling parent reply Carlos Santander <csantander619 gmail.com> writes:
Trevor Parscal escribió:
 I just finished binding a windowing library, Cpw (http://www.mathies.com/cpw/),
 to D, in a style simular to Derelict, meaning I use a loader to connect
function
 pointers to the functions in a DLL that was loaded into memory dynamicly.
 
 The library works, sort of. I can get it to perform initialization and it will
 create a window, however once it has to use the callbacks, if they use the
 pointer to the Cpw context for anything, i get an access violation.
 
 I will clarify.
 
 extern(C)
 {
 void windowCallback(pCpw cpw, uint winid, bit flag)
 {
 if(!flag)
 {
 // Close the Window (! This is where it crashes !)
 cpwDestroyWindow(cpw, winid);
 }
 }
 }
 
 int main(char[][] args)
 {
 // Load the DLL
 cpwLoad();
 
 // Create and Initialize a Cpw Context
 pCpw cpw;
 cpwInitContext(&cpw)
 
 // Create a Window
 cpwCreateWindowEx(cpw, "Basic Template", 200, 200, 640, 480);
 
 // Set Callback
 cpwCreateCallback(cpw, cast(CpwCreateCallback)&windowCallback);
 
 // Run
 cpwMainLoop(cpw);
 cpwFreeContext(&cpw);
 return 0;
 }
 
 So when you start a Cpw program, you create a pCpw, which is a pointer to a Cpw
 context struct. Than you initialize it. You can create a window, and than set a
 callback for different events of that window.
 
 My problem is when the callback gets called, the pointer that Cpw is sending to
 the function to identify the conext is for some reason not accessable to the
 callback function, thus I get an Access Violation error.
 
 If I don't make the callback functions in extern(C), I get an access violation
 whenever Cpw tries to call the callback functions. If I don't specify a
 callback, everything works fine, cause Cpw has defaults, that I guess do have
 access to the context pointer, unlike mine.
 
 I have tried defining the cpw pointer in the extern(C) area, but it makes no
 difference.
 
 Why do my callbacks not have access to the context pointer?
 
 Thanks in advance for any and all help!
 
 Thanks,
 Trevor Parscal
 www.trevorparscal.com
 trevorparscal hotmail.com

Assuming pCpw is defined as alias Cpw* pCpw; Then you could try this: pCpw cpw = new Cpw; Sometimes I've had to do that interfacing with Win32. I don't know if it'll solve your problem, but you could try. -- Carlos Santander Bernal
May 27 2005
parent Trevor Parscal <Trevor_member pathlink.com> writes:
I am still using extern(C), but I got it to work finnally. I found some problems
with the loader...

HOWEVER, Now I am running into another access violation. OpenGL. I am using the
Derelict opengl library, which i have used with other toolkits and directly with
native windows just fine.

Everytime I make a gl call, i get an access violation. The error that Cpw throws
is that it's an invalid context.

Maybe this has something to do with interfacing the C code, maybe it has to do
with Cpw being messed up. I am frustrated either way...

Any ideas? I have NEVER had this problem before.

Thanks,
Trevor Parscal
www.trevorparscal.com
trevorparscal hotmail.com
May 27 2005