• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 619
  • Last Modified:

P/Invoke on Embedded SQL C Program throws AccessViolationException

I've figured out how to "export" a function in my Embedded SQL C program so that it's visible in C# through P/Invoke ("__declspec(dllexport)" was the trick). So far, everything runs great to a point... I can run "dumpbin /exports myEmbeddedCode.exe" and see my function listed there - sweet! The problem is that as soon as I try to call the function, I get an AccessViolationException ("Attempted to read or write protected memory. This is often an indication that other memory is corrupt.").

This is the C code:


/* this very simple function is just to verify that my export/pinvoke works */int __declspec(dllexport) myAdd(int x, int y){      return x + y;}/*This is the function that won't run - I keep getting an AccessViolationException */int __declspec(dllexport) getInt(){      EXEC SQL CONNECT TO MYDB;      EXEC SQL SELECT CLOSED INTO :Q_long FROM BASE WHERE FLAG = '1';      EXEC SQL CONNECT RESET;      return Q_long;}


Here is the .Net code that accesses these two functions:


[DllImport(@"c:\embeddedSQL.exe",             EntryPoint = "myAdd",            ExactSpelling = false,             CallingConvention = CallingConvention.Cdecl)]static extern int myAdd(int a, int b);[DllImport(@"c:\embeddedSQL.exe",             EntryPoint = "getInt",            ExactSpelling = false,             CallingConvention = CallingConvention.Cdecl)]static extern int getInt();private void Form1_Load(object sender, EventArgs e){    /* this one works perfectly */    MessageBox.Show("Sum of 5 and 2 is " + myAdd(5, 2));    /* this one throws an AccessViolationException */    int closed = getInt();}


Any ideas on what I should be doing or what's going wrong?

Thanks!
0
helpatudallshumwaycom
Asked:
helpatudallshumwaycom
  • 2
1 Solution
 
josgoodCommented:
Would you post your exact C code for getint please?  "EXEC SQL CONNECT TO MYDB;" isn't valid in the code you presented.

If getint() is defined as, for example,
    int __declspec(dllexport) getInt(){      
       return 42;
    }
Then your C# code will run without error.
0
 
helpatudallshumwaycomAuthor Commented:
This is embedded SQL code that runs against a DB2 database.  The code gets prepped where the "EXEC" statements are replaced with the actual API calls.  Here's the actual C code:

int __declspec(dllexport) jonoGetClosedDate()
{
/*
EXEC SQL CONNECT TO JONATHAN USER ASL USING ASLICDB;
*/

{
  sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);
  sqlaaloc(2,3,1,0L);
    {
      struct sqla_setdata_list sql_setdlist[3];
      sql_setdlist[0].sqltype = 460; sql_setdlist[0].sqllen = 7;
      sql_setdlist[0].sqldata = (void*)"DBNAME";
      sql_setdlist[0].sqlind = 0L;
      sql_setdlist[1].sqltype = 460; sql_setdlist[1].sqllen = 4;
      sql_setdlist[1].sqldata = (void*)"USR";
      sql_setdlist[1].sqlind = 0L;
      sql_setdlist[2].sqltype = 460; sql_setdlist[2].sqllen = 5;
      sql_setdlist[2].sqldata = (void*)"PSWD";
      sql_setdlist[2].sqlind = 0L;
      sqlasetdata(2,0,3,sql_setdlist,0L,0L);
    }
  sqlacall((unsigned short)29,5,2,0,0L);
  sqlastop(0L);
}
      
/*
EXEC SQL SELECT CLOSED INTO :Q_long FROM BASE WHERE FLAG = '1';
*/
{
  sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);
  sqlaaloc(3,1,2,0L);
    {
      struct sqla_setdata_list sql_setdlist[1];
      sql_setdlist[0].sqltype = 496; sql_setdlist[0].sqllen = 4;
      sql_setdlist[0].sqldata = (void*)&Q_long;
      sql_setdlist[0].sqlind = 0L;
      sqlasetdata(3,0,1,sql_setdlist,0L,0L);
    }
  sqlacall((unsigned short)24,3,0,3,0L);
  sqlastop(0L);
}
      
/*
EXEC SQL CONNECT RESET;
*/
{
  sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);
  sqlacall((unsigned short)29,3,0,0,0L);
  sqlastop(0L);
}
        return Q_long;
}
0
 
josgoodCommented:
I don't have DB2 here, nor am I familiar with it, but let me give this my best shot.

1)  If you replace "return Q_long" with "return 7", do you still get the access violation?
2)  If you do, then work your way up the function, commenting out a few lines each time until you no longer get the access violation.
3)  If you do not, then how is Q_long initialized?

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now