Question

I have to access some functions from a DLL Library in C#. My First step was create a wrapper class to deal with it:

//Class TMDLLWrapper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace TrueMarbleData
{
  class TMDLLWrapper
  {

    [DllImport("TrueMarbleDLL.dll")]
    public static extern int GetTileSize(out int width, out int height);

    [DllImport("TrueMarbleDLL.dll")]
    public static extern int GetNumTiles(int zoomLevel, out int numTilesX, out int numTilesY);

    [DllImport("TrueMarbleDLL.dll")]
    public static extern int GetTileImageAsRawJPG(int zoomLevel, int tileX, int tileY, out byte imageBuf, int bufSize, out int jpgSize);
  }


}

Then, I've created an interface and a class to access wrapper class' methods:

//Interface ITMDataController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace TrueMarbleData
{
   [ServiceContract]
   public interface ITMDataController
   {       
       [OperationContract]
       int GetTileWidth();

       [OperationContract]
       int GetTileHeight();

       [OperationContract]
       int GetNumTilesAcross(int zoom);

       [OperationContract]
       int GetNumTilesDown(int zoom);

       [OperationContract]
       byte[] LoadTile(int zoom, int x, int y);
   }
}


//Class TMDataControllerImpl.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using TrueMarbleData;

 namespace TrueMarbleData
 {
     [ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple,
                      UseSynchronizationContext=false)]
     internal class TMDataControllerImpl : ITMDataController
     {

           TMDLLWrapper obj = new TMDLLWrapper(); //Problem here: I cannot access 
                                                  //wrapper class' methods
                                                  //Methods available: Equals, 
                                                  //GetHashCode, GetType, ToString
           public int GetTileWidth()
           {
              return 0;
           }

           public int GetTileHeight();

           public int GetNumTilesAcross(int zoom);

           public int GetNumTilesDown(int zoom);

           public byte[] LoadTile(int zoom, int x, int y);
    }
 }

I need to access the Wrapper Class' methods from the TMDataControllerImpl class. However, when I create an object from TMDLLWrapper class, I can only access these methods: Equals, GetHashCode, GetType, ToString. It's supposed to be a easy thing to do, and I have not figured out where the error is. Anyone can help me?

Was it helpful?

Solution

classes are internal by default. Make the wrapper class public:

public class TMDLLWrapper
{
...
}

You would then access it's methods in a static fashion:

TMDLLWrapper.GetNumTiles(...);

OTHER TIPS

Since methods in TMDLLWrapper are static, you should use syntax for access static methods:

TMDLLWrapper.GetTileSize(/* ... */);

You don't need to create instance of TMDLLWrapper. Moreover, it is desirable to make it static class:

static class TMDLLWrapper { /* ... */ }

When you try to acces methods this way:

TMDLLWrapper obj = new TMDLLWrapper(); 

You can only access the instance methods. So you should use the static methods the static way:

TMDLLWrapper.GetTileSize(a, b);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top