I created a simple DLL in C# (using Visual Studio 2017) and I'm trying to call it from a stored procedure in Sybase 17.

When I call the Store Procedures, I received an error message saying that method could not be found in the DLL. It looks like the methods inside the DLL is not visible outside?!

Sybase Procedures:

CREATE PROCEDURE "mystring"()
External name 'MyMethodTEst@c:\\D\\AmosTest.dll' language "C_ESQL64"

Call the store Procedure:

CALL "amos"."mystring"()

Sybase Error:

-- Could not execute statement. -- Procedure 'mystring' terminated with unhandled exception 'Could not find --'MyMethodTEst' in dynamic library 'c:\D\AmosTest.dll'' -- SQLCODE=-91, ODBC 3 State="HY000" -- (Continuing after error) -- Procedure completed

DLL Source code:

using System;
using System.Runtime.InteropServices;

namespace AmosTest {
    [ComVisible(true)]
    [ProgId("AmosTest.Class1")]
    public class Class1 {
        public string MyMethodTEst(string param) {
            return "OK!!" ;
        }
    }
}

asked 15 Oct, 02:42

Ale4171's gravatar image

Ale4171
111
accept rate: 0%

edited 15 Oct, 06:28

Volker%20Barth's gravatar image

Volker Barth
36.9k343505765

1

Your CREATE PROCEDURE statement would fit for a C/C++ external function, not for a CLR method. For native CLR functions and procedures you need to specify "LANGUAGE CLR", and the class name must be added, as well. Confine the sample from the docs.

(15 Oct, 04:28) Volker Barth

Thanks a lot for you feedback.

I Have modified the Store Procedure:

ALTER PROCEDURE mystring() external name 'Class1.MyMethodTEst@c:\D\AmosTest.dll' language CLR

BUT I've got this error:

CALL mystring()

-- Could not execute statement. -- Procedure 'mystring' terminated with unhandled exception 'Invalid method -- signature: 5AmosTest.Class1.MyMethodTEst@c:\D\AmosTest.dll' -- SQLCODE=-91, ODBC 3 State="HY000" -- (Continuing after error) -- Procedure completed

Any Idea? Thanks again in advances. Ale

(15 Oct, 05:41) Ale4171
Replies hidden

Can't tell whether the path/location is correct, however, AFAIK all CRL methods must be declared static when called from SQL Anywhere, cf. the samples.

In other words: I'd recommend to try to run a given sample - and once that does work, try my own approaches...

(15 Oct, 06:29) Volker Barth

Hi Volker

First of all thanks again, now I fixed, the problem was the .NET project. I create a new project in c# and not in C++ and now when I Compile the DLL it works fine. Thanks again for your help!

(15 Oct, 08:57) Ale4171

The functions have to be declared static and your external procedure definition is declared incorrectly as it is not being supplied a parameter ( and should really be a function since you are returning a value). That said, the key issue for the error is that the method is AmosTest.Class1.MyMethodTEst (the namespace, class, and method).

The correct C# code is:

using System.Runtime.InteropServices;

namespace AmosTest {
    [ComVisible(true)]
    [ProgId("AmosTest.Class1")]
    public  class Class1 {
        public static string MyMethodTEst(string param) {
            return "OK!!" ;
        }
    }
}

And the function in SQL Anywhere should be:

create or replace function mystring( in c long varchar ) 
    returns long varchar 
        external name 'AmosTest.dll::AmosTest.Class1.MyMethodTEst(string) string' 
    language clr;

And called like:

select mystring('Hi!');
permanent link

answered 15 Oct, 09:40

Chris%20Keating's gravatar image

Chris Keating
5.2k2987
accept rate: 33%

edited 15 Oct, 09:41

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×23
×19
×10
×3

question asked: 15 Oct, 02:42

question was seen: 64 times

last updated: 15 Oct, 09:41