problems getting a value which is being passed into an extended stored proc as an int

  • I am having problems getting a value which is being passed into an extended stored proc(XP) (as an int)
     
    For example,  in query analyzer:
     
    DECLARE @retval int

    EXEC @retval = master..xp_test 255

    SELECT @retval           -- returns 255 (OK)     

    EXEC @retval = master..xp_test 257

    SELECT @retval           -- returns 1  (WHY?)

     
    This tells me that it is only looking at the first byte, not all 4 bytes when converting.  I suspect an UNICODE issue but not sure what to do.  code is in VS2003, C++.
     
    Any suggestions?
     

    Proc = srvproc;

    LONG forit = 0;

    BYTE bType = 0;

    ULONG ulMaxLen = 0;

    ULONG ulActualLen = 0;

    BOOL isParamNull = FALSE;

    int inputParamIndex = 1;

    rc = srv_paraminfo(Proc, inputParamIndex, &bType

    , &ulMaxLen, &ulActualLen, NULL, &isParamNull);

    // Create memory to get the long parameters into.

    BYTE* ldata = new BYTE[ulMaxLen];

    memset(ldata, '\0', ulMaxLen);

    rc = srv_paraminfo(Proc, inputParamIndex, &bType

    , &ulMaxLen, &ulActualLen, ldata, &isParamNull);

    forit = (LONG)*ldata;

    delete []ldata;

    return forit;

  • If you use constant as parameter, you can try to cast it to int. Or declare a variable.

    DECLARE @retval int

    DECLARE @Param int

    SET @Param=255

    EXEC @retval = master..xp_test @Param

    SELECT @retval           -- returns 255 (OK)     

    SET @Param=257

    EXEC @retval = master..xp_test @Param

    SELECT @retval    

     

  • Alas, if it were only that easy, but that does not work either.  The problem is in the C code itself.

    Thanks

  • Can you print the ulActualLen and ldata after get the parameter in the xp by srv_sendmsg?

     

  • I am walking through with the debugger -

    bType = 38

    ulMaxLen = 4

    ulActualLen = 4

    lData = 2

    szServerCodePage  = "850" 

  • I have figured it out, and it is indeed my error.  I was defining ldata as a BYTE*, not a ULONG*

    Corrected code fragment is:

    ULONG* ldata = new ULONG[ulMaxLen];

    memset(ldata, '\0', ulMaxLen);

    rc = srv_paraminfo(Proc, inputParamIndex, &bType

    , &ulMaxLen, &ulActualLen, (BYTE*)ldata, &isParamNull);

    forit = (LONG)*ldata;

    delete []ldata;

     

    Thanks

    Ric

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply